"Fossies" - the Fresh Open Source Software Archive

Member "sitecopy-0.16.6/gnome/gcommon.c" (24 Apr 2005, 14562 Bytes) of archive /linux/www/sitecopy-0.16.6.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "gcommon.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  *      XSitecopy, for managing remote web sites with a GNOME interface.
    3  *      Copyright (C) 2000, Lee Mallabone <lee@fonicmonkey.net>
    4  *
    5  *      This program is free software; you can redistribute it and/or modify
    6  *      it under the terms of the GNU General Public License as published by
    7  *      the Free Software Foundation; either version 2 of the License, or
    8  *      (at your option) any later version.
    9  *
   10  *      This program is distributed in the hope that it will be useful,
   11  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  *      GNU General Public License for more details.
   14  *
   15  *      You should have received a copy of the GNU General Public License
   16  *      along with this program; if not, write to the Free Software
   17  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18  *
   19  */
   20 
   21 /* This file implements all the functions required by sitecopy in
   22  * src/frontend.h and also provides a few convenience functions for fun.
   23  */
   24 
   25 #include <stdio.h>
   26 #include <stdlib.h>
   27 #include <semaphore.h>
   28 #include "dirname.h"
   29 #include "rcfile.h"
   30 #include "gcommon.h"
   31 
   32 #include "config.h"
   33 
   34 static gint button;
   35 
   36 GtkWidget *connection_label = NULL;
   37 
   38 extern sem_t *update_semaphore;
   39 
   40 extern struct site *selected_site;
   41 extern GtkWidget *error_log_list, *error_log_window;
   42 
   43 /* For the 'main' upload window */
   44 extern GtkWidget *main_progressbar, *job_progressbar, *error_button;
   45 extern GtkWidget *status_label, *op_label, *file_label, *dir_label;
   46 extern GtkWidget *begin_button;
   47 
   48 extern struct site *all_sites;
   49 
   50 /* This value is what makes the entire "job" progress bar possible. */
   51 float upload_total, uploaded_bytes = 0.0;
   52 
   53 extern GtkWidget *sitecopy;
   54 
   55 /*************************************************/
   56 
   57 /* Returns TRUE if *oldString _was_ replaced with replacementString, or
   58  * FALSE if NULL was substituted.
   59  */
   60 gboolean xsc_replace_string(char **oldString, char *replacementString)
   61 {
   62 
   63     /* Check the value of replacementString. If it's NULL, or an empty
   64      * string, then set *oldString to be NULL also.
   65      */
   66    if ( (!replacementString) || (strlen(replacementString) < 1) )
   67      {
   68     *oldString = NULL;
   69     return FALSE;
   70      }
   71 
   72     /* If oldString is valid, then optionally free any memory used by
   73      * *oldString, and then point oldString to replacementString
   74      */
   75    if (*oldString)
   76      {
   77     free(*oldString);
   78      }
   79    *oldString = replacementString;
   80    return TRUE;
   81 }
   82 
   83 /* Searches for the glade file, and loads a static variable with the location.
   84  * Once found, the variable is returned straight away.
   85  */
   86 
   87 gchar *get_glade_filename(void)
   88 {
   89    static gchar *theFilename = NULL;
   90 
   91    if (theFilename)
   92      return theFilename;
   93 
   94     /* Search for the glade file in $(datadir)/xsitecopy/, ./ and ../ */
   95    theFilename = gnome_datadir_file("xsitecopy/sitecopy-dialogs.glade");
   96    if ((!theFilename) || (!g_file_exists(theFilename)))
   97      {
   98     g_free(theFilename);
   99     theFilename = g_strdup("sitecopy-dialogs.glade");
  100     if (!g_file_exists(theFilename))
  101       {
  102          g_free(theFilename);
  103          theFilename = g_strdup("../sitecopy-dialogs.glade");
  104          if (!g_file_exists(theFilename))
  105            {
  106           g_error("Can not find sitecopy-dialogs.glade, exiting.\n");
  107           theFilename = NULL;
  108            }
  109       }
  110      }
  111    return theFilename;
  112 }
  113 
  114 int fe_gtk_question(char *question, GnomeReplyCallback yes_action)
  115 {
  116    GtkWidget *question_window;
  117    question_window = gnome_app_question(GNOME_APP(sitecopy),
  118                     (const gchar *) question,
  119                     yes_action, NULL);
  120    gtk_widget_show(question_window);
  121    return 1;
  122 }
  123 
  124 void fe_transfer_progress(off_t progress, off_t total)
  125 {
  126    float div1, div2;
  127 
  128     /* Update the current file's progress */
  129    div1 = (float) progress;
  130    div2 = (float) total;
  131    gdk_threads_enter();
  132     /* Let's not do anything silly like divide by zero. */
  133    if (div2)
  134      gtk_progress_bar_update(GTK_PROGRESS_BAR(main_progressbar), div1 / div2);
  135 
  136     /* Update total job progress */
  137    gtk_progress_bar_update(GTK_PROGRESS_BAR(job_progressbar),
  138                (uploaded_bytes + (float) progress) / (float) upload_total);
  139    gdk_threads_leave();
  140 
  141 }
  142 
  143 void gfe_status(const char *message)
  144 {
  145    gnome_app_message(GNOME_APP(sitecopy), (const gchar *) message);
  146 }
  147 
  148 void fe_warning(const char *description, const char *subject,
  149         const char *error)
  150 {
  151 /*    gnome_warning_dialog(description);*/
  152 }
  153 
  154 /* The user is required to authenticate themselves for given context,
  155  * in the given realm on the given hostname. This is achieved by forming an
  156  * appropriate label givne the context, realm and hostname. A dialog is
  157  * then constructed with libglade. run_and_close() is performed on the
  158  * dialog, then some sanity checking for what was entered. Finally, the
  159  * 'out' parameters of the function are set, the gtk+ lock is removed, and
  160  * the appropriate value returned.
  161  *
  162  */
  163 
  164 /* Thanks to David Knight. This function was copied almost verbatim from the screem upload wizard.
  165  */
  166 
  167 int
  168   fe_login( fe_login_context ctx, const char *realm, const char *hostname,
  169        char *username, char *password )
  170 {
  171    GtkWidget *widget;
  172    gchar *tmp;
  173    gchar const *server_type;
  174    GladeXML *login_xml;
  175 
  176    g_print("fe_login");
  177    gdk_threads_enter();
  178 
  179    switch (ctx)
  180      {
  181       case fe_login_server:
  182     server_type = "remote";
  183     break;
  184       case fe_login_proxy:
  185     server_type = "proxy";
  186     break;
  187 #ifndef NDEBUG
  188       default:
  189     fprintf(stderr, "fe_login: unknown ctx %d.  Aborting.\n", (int) ctx);
  190     abort();
  191 #endif /* !NDEBUG */
  192      }
  193 
  194     /* Form a sane prompt for details regardless of whether a realm is
  195      * provided or not.
  196      */
  197    tmp = g_strdup_printf("The %s server requires authentication.\n\n"
  198              "Please enter details for %s%s%s.",
  199              server_type,
  200              realm?realm:"",
  201              realm?" at ":"",
  202              hostname);
  203 
  204    login_xml = glade_xml_new((const char *)get_glade_filename(),
  205                  "user_pass_dialog");
  206    gtk_label_set(GTK_LABEL(glade_widget(login_xml, "user_pass_prompt_label")),
  207          tmp);
  208 
  209    g_free(tmp);
  210 
  211    if( username[0] != '\0' )
  212      {
  213     widget = glade_xml_get_widget( login_xml, "user_entry" );
  214     gtk_entry_set_text( GTK_ENTRY( widget ), username );
  215      }
  216 
  217    widget = glade_xml_get_widget(login_xml, "user_pass_dialog");
  218    gtk_window_set_modal(GTK_WINDOW(widget), TRUE);
  219    gtk_widget_show_all(widget);
  220    glade_xml_signal_autoconnect(login_xml);
  221 
  222    button = -1;
  223    gdk_threads_leave();
  224 
  225    sem_wait(update_semaphore);
  226 
  227    if( button != 0 )
  228      {
  229         /* canceled */
  230     gtk_widget_destroy( widget );
  231     return -1;
  232      }
  233    gdk_threads_enter();
  234    gtk_window_set_modal(GTK_WINDOW(widget), FALSE);
  235    widget = glade_xml_get_widget( login_xml, "user_entry" );
  236    strncpy(username, gtk_entry_get_text( GTK_ENTRY( widget ) ),
  237        FE_LBUFSIZ);
  238 
  239    widget = glade_xml_get_widget( login_xml, "pass_entry" );
  240    strncpy(password, gtk_entry_get_text( GTK_ENTRY( widget ) ),
  241        FE_LBUFSIZ);
  242 
  243    widget = glade_xml_get_widget( login_xml, "user_pass_dialog" );
  244    gtk_widget_destroy( widget );
  245 
  246    gdk_threads_leave();
  247 
  248    return 0;
  249 }
  250 
  251 
  252 void fe_login_clicked( GnomeDialog *dialog, gint number )
  253 {
  254            button = number;
  255            sem_post(update_semaphore);
  256 }
  257 
  258 void fe_login_close( GnomeDialog *dialog )
  259 {
  260            sem_post(update_semaphore);
  261 }
  262 
  263 
  264 void fe_connection( fe_status status, const char *info)
  265 {
  266    char *tmp;
  267 
  268    gdk_threads_enter();
  269    switch (status)
  270      {
  271       case (fe_namelookup):
  272     if (info)
  273       {
  274          tmp = g_strdup_printf("Looking up %s...", info);
  275          gtk_label_set(GTK_LABEL(connection_label), tmp);
  276          g_free(tmp);
  277       }
  278     else
  279       {
  280          gtk_label_set(GTK_LABEL(connection_label), "Looking up hostname...");
  281       }
  282     break;
  283       case (fe_connecting):
  284     gtk_label_set(GTK_LABEL(connection_label), "Attempting to connect...");
  285     break;
  286       case (fe_connected):
  287     gtk_label_set(GTK_LABEL(connection_label), "Connected.");
  288     break;
  289      }
  290    gdk_threads_leave();
  291 }
  292 
  293 int fe_can_update(const struct site_file *file)
  294 {
  295    g_print("Confirmation given to upload file %s.\n",
  296        file->local.filename);
  297    return TRUE;
  298 }
  299 
  300 void fe_verified(const char *fname, enum file_diff match)
  301 {
  302     /* TODO */
  303 }
  304 
  305 void fe_updating(const struct site_file *file)
  306 {
  307    char *file_status;
  308 
  309    gdk_threads_enter();
  310    file_status = g_strdup_printf("Commiting updates to %s...",
  311                  selected_site->server.hostname);
  312    gtk_label_set(GTK_LABEL(status_label), file_status);
  313     /* Check this */
  314    g_free(file_status);
  315    if (file->type == file_dir)
  316      {
  317     if (file->diff == file_new)
  318       {
  319          gtk_label_set(GTK_LABEL(op_label), "Creating directory...");
  320          gtk_label_set(GTK_LABEL(file_label), file_name(file));
  321         /*         gtk_label_set (GTK_LABEL (dir_label), file->directory); */
  322          gtk_label_set(GTK_LABEL(dir_label), "");
  323       }
  324     else
  325       {
  326         /* can we move dirs yet? */
  327          gtk_label_set(GTK_LABEL(op_label), "Deleting directory...");
  328          gtk_label_set(GTK_LABEL(dir_label), "");
  329       }
  330      }
  331    else
  332      {
  333     switch (file->diff)
  334       {
  335        case file_changed:
  336          gtk_label_set(GTK_LABEL(op_label), "Uploading...");
  337          gtk_label_set(GTK_LABEL(file_label), file_name(file));
  338          gtk_label_set(GTK_LABEL(dir_label), dir_name(file_name(file)));
  339          break;
  340        case file_new:
  341          gtk_label_set(GTK_LABEL(op_label), "Uploading...");
  342          gtk_label_set(GTK_LABEL(file_label), file_name(file));
  343          gtk_label_set(GTK_LABEL(dir_label), dir_name(file_name(file)));
  344          break;
  345        case file_deleted:
  346          gtk_label_set(GTK_LABEL(op_label), "Deleting...");
  347          gtk_label_set(GTK_LABEL(file_label), file_name(file));
  348          gtk_label_set(GTK_LABEL(dir_label), "");
  349          break;
  350        case file_moved:
  351          gtk_label_set(GTK_LABEL(op_label), "Moving...");
  352          gtk_label_set(GTK_LABEL(file_label), file_name(file));
  353         /* FIXME: Check this, I think it's dodgy. */
  354          gtk_label_set(GTK_LABEL(dir_label), dir_name(file_name(file)));
  355          break;
  356        case file_unchanged:
  357          gtk_label_set(GTK_LABEL(op_label), "ARG! The file hasn't changed, we shouldn't be doing anything!");
  358       }
  359      }
  360    gdk_threads_leave();
  361 }
  362 
  363 /* Once a file has been updated, any errors with it are recorded in the
  364  * error list, and the progress bar is reset.
  365  */
  366 void fe_updated(const struct site_file *file, int success,
  367         const char *error)
  368 {
  369    gchar *error_item[2];
  370 
  371    gdk_threads_enter();
  372 
  373    if (!success)
  374      {
  375 
  376         g_assert(error!=NULL);
  377 
  378         error_item[0] = file_name(file);
  379     error_item[1] = g_strdup(error);
  380 
  381     gtk_clist_append(GTK_CLIST(error_log_list), error_item);
  382 
  383         NE_DEBUG(DEBUG_GNOME, "Error \"%s\" with file: %s\n",
  384           error, file_name(file));
  385     g_free(error_item[1]);
  386      }
  387    gtk_progress_bar_update(GTK_PROGRESS_BAR(main_progressbar), 0.0);
  388 
  389    /* If the file exists locally, update how many bytes have globally
  390     * been updated */
  391    if (file->local.filename)
  392      uploaded_bytes += (float) file->local.size;
  393 
  394    gdk_threads_leave();
  395 }
  396 
  397 int verifysite_gnome(struct site *a_site)
  398 {
  399    int ret = rcfile_verify(a_site);
  400    if (!ret)
  401      return 0;
  402 
  403    switch (ret)
  404      {
  405       case SITE_NOSERVER:
  406     gnome_error_dialog("Server not specified.");
  407     break;
  408       case SITE_NOREMOTEDIR:
  409     gnome_error_dialog("Remote directory not specified.");
  410     break;
  411       case SITE_NOLOCALDIR:
  412     gnome_error_dialog("Local directory not specified.");
  413     break;
  414       case SITE_ACCESSLOCALDIR:
  415     gnome_error_dialog("Could not read local directory.");
  416     break;
  417       case SITE_INVALIDPORT:
  418     gnome_error_dialog("Invalid port.");
  419     break;
  420       case SITE_NOMAINTAIN:
  421     gnome_error_dialog("The chosen protocol cannot maintain symbolic links");
  422     break;
  423       case SITE_NOREMOTEREL:
  424     gnome_error_dialog("The chosen protocol cannot use a relative remote directory.");
  425     break;
  426       case SITE_NOPERMS:
  427     gnome_error_dialog("The protocol you are attempting to use does\nnot currently support maintaining permissions.");
  428     break;
  429       case SITE_NOLOCALREL:
  430     gnome_error_dialog("Could not use a 'relative' local directory");
  431       default:
  432     gnome_error_dialog("There was an undetermined problem verifying the correctness of your site definition. Please report this to the maintainer.");
  433     break;
  434      }
  435    return ret;
  436 }
  437 
  438 /* Coming soon... */
  439 void fe_synching(const struct site_file *file)
  440 {
  441 }
  442 
  443 void fe_synched(const struct site_file *file, int success,
  444         const char *error)
  445 {
  446 }
  447 
  448 void fe_setting_perms(const struct site_file *file)
  449 {
  450 }
  451 void fe_set_perms(const struct site_file *file, int success,
  452           const char *error)
  453 {
  454 }
  455 
  456 /* This function keeps the handling of return codes from site_update,
  457  * site_fetch and site_resync consistant, as they all return the same
  458  * potential values.
  459  */
  460 
  461 void set_status_after_operation(int return_code,
  462                 GtkLabel *infoLabel)
  463 {
  464    g_assert(infoLabel != NULL);
  465 
  466    switch (return_code)
  467      {
  468       case SITE_OK:
  469     gtk_label_set(GTK_LABEL(infoLabel), "Operation succesfully completed.");
  470     site_write_stored_state(selected_site);
  471     NE_DEBUG (DEBUG_GNOME, "Site fetch was successful.\n");
  472     break;
  473       case SITE_LOOKUP:
  474     gtk_label_set(GTK_LABEL(infoLabel), "Host name lookup failed.");
  475     NE_DEBUG (DEBUG_GNOME, "lookup failed\n");
  476     break;
  477       case SITE_PROXYLOOKUP:
  478     gtk_label_set(GTK_LABEL(infoLabel), "Host name lookup for the proxy server failed.");
  479     NE_DEBUG (DEBUG_GNOME, "lookup failed\n");
  480     break;
  481       case SITE_CONNECT:
  482     gtk_label_set(GTK_LABEL(infoLabel), "Could not connect to remote site.");
  483     NE_DEBUG (DEBUG_GNOME, "no connection.\n");
  484     break;
  485       case SITE_ERRORS:
  486     gtk_label_set(GTK_LABEL(infoLabel), "There were errors during the transfer.");
  487     NE_DEBUG (DEBUG_GNOME, "no connection.\n");
  488     break;
  489       case SITE_AUTH:
  490     gtk_label_set(GTK_LABEL(infoLabel), "Could not authenticate you with the remote server.");
  491     NE_DEBUG (DEBUG_GNOME, "Could not authenticate\n");
  492     break;
  493       case SITE_PROXYAUTH:
  494     gtk_label_set(GTK_LABEL(infoLabel), "Could not authenticate you with the proxy server.");
  495     NE_DEBUG (DEBUG_GNOME, "Could not do proxy authenticate\n");
  496     break;
  497       case SITE_FAILED:
  498     gtk_label_set(GTK_LABEL(infoLabel), "The operation failed. No reason was given.");
  499     NE_DEBUG (DEBUG_GNOME, "Fetch failed.\n");
  500     break;
  501       case SITE_UNSUPPORTED:
  502     gtk_label_set(GTK_LABEL(infoLabel), "This operation is unsupported for the selected protocol.");
  503     NE_DEBUG (DEBUG_GNOME, "NOt implemented!\n");
  504     break;
  505       case SITE_ABORTED:
  506     gtk_label_set(GTK_LABEL(infoLabel), "Operation aborted.");
  507     NE_DEBUG (DEBUG_GNOME, "Um. We got aborted HERE?!\n");
  508     break;
  509       default:
  510     gtk_label_set(GTK_LABEL(infoLabel), "This should never appear. Please contact the maintainer.");
  511     NE_DEBUG (DEBUG_GNOME, "default.\n");
  512      }
  513 }
  514