GnuCash 2.3.0
druid-gconf-setup.c
Go to the documentation of this file.
00001 /*
00002  * druid-gconf-setup.c  -- install gconf keys where they can be found.
00003  *
00004  * Copyright (c) 2005 David Hampton <hampton@employees.org>
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, contact:
00018  *
00019  * Free Software Foundation           Voice:  +1-617-542-5942
00020  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00021  * Boston, MA  02110-1301,  USA       gnu@gnu.org
00022  */
00023 
00033 #include "config.h"
00034 
00035 #include <gnome.h>
00036 #include <glib/gi18n.h>
00037 #include <glib/gstdio.h>
00038 #include <stdlib.h>
00039 #include <errno.h>
00040 #include <sys/types.h>
00041 
00042 #include "dialog-utils.h"
00043 #include "druid-gconf-setup.h"
00044 #include "druid-utils.h"
00045 #include "gnc-path.h"
00046 #include "gnc-gconf-utils.h"
00047 #include "gnc-gui-query.h"
00048 #include "gnc-gnome-utils.h"
00049 #include "gnc-ui.h"
00050 
00051 #define WHO_DOES                "who_does"
00052 #define WHO_GNUCASH             1
00053 #define WHO_USER                2
00054 #define WHO_ALREADY_DONE        3
00055 
00056 #define HOW                     "how"
00057 #define HOW_UPDATE              1
00058 #define HOW_INSTALL             2
00059 
00060 #define SCRIPT_NAME "update-gnucash-gconf"
00061 
00062 #define PATH_STRING1 "xml:readwrite:$(HOME)/.gconf\n"
00063 #define PATH_STRING2 "xml:readonly:%s\n"
00064 
00065 
00066 /********************
00067  * Declarations
00068  ********************/
00069 
00070 
00071 gboolean druid_gconf_delete_event (GtkWidget *window, GdkEvent *event, gpointer user_data);
00072 void     druid_gconf_cancel (GnomeDruid *druid, gpointer user_data);
00073 void     druid_gconf_choose_page_prepare (GnomeDruidPage *druidpage, GnomeDruid *druid, gpointer user_data);
00074 gboolean druid_gconf_choose_page_next (GnomeDruidPage *druidpage, GnomeDruid *druid, gpointer user_data);
00075 void     druid_gconf_update_page_prepare (GnomeDruidPage *druidpage, GnomeDruid *druid, gpointer user_data);
00076 gboolean druid_gconf_update_page_next (GnomeDruidPage *druidpage, GnomeDruid *druid, gpointer user_data);
00077 void     druid_gconf_install_page_prepare (GnomeDruidPage *druidpage, GnomeDruid *druid, gpointer user_data);
00078 gboolean druid_gconf_install_page_next (GnomeDruidPage *druidpage, GnomeDruid *druid, gpointer user_data);
00079 gboolean druid_gconf_install_page_back (GnomeDruidPage *druidpage, GnomeDruid *druid, gpointer user_data);
00080 void     druid_gconf_finish_page_prepare (GnomeDruidPage *druidpage, GnomeDruid *druid, gpointer user_data);
00081 gboolean druid_gconf_finish_page_back (GnomeDruidPage *druidpage, GnomeDruid *druid, gpointer user_data);
00082 void     druid_gconf_finish_page_finish (GnomeDruidPage *druidpage, GnomeDruid *druid, gpointer user_data);
00083 
00084 
00085 /********************
00086  * Work Functions
00087  ********************/
00088 
00089 
00110 static gboolean
00111 druid_gconf_update_path (GError **error)
00112 {
00113     gchar *path_filename, *data_filename;
00114     gchar *contents, **lines, *line;
00115     gboolean found_user_dir = FALSE;
00116     FILE *output;
00117     gchar *gconfdir;
00118 
00119     data_filename = g_build_filename(g_get_home_dir(), ".gconf", (char *)NULL);
00120     path_filename = g_build_filename(g_get_home_dir(), ".gconf.path", (char *)NULL);
00121     if (g_file_test(path_filename, G_FILE_TEST_EXISTS))
00122     {
00123         if (!g_file_get_contents(path_filename, &contents, NULL, error))
00124         {
00125             g_free(path_filename);
00126             g_free(data_filename);
00127             return FALSE;
00128         }
00129 
00130         lines = g_strsplit_set(contents, "\r\n", -1);
00131         for (line = *lines; line; line++)
00132         {
00133             if (line[0] == '#')
00134                 continue;
00135             if ((strstr(line, "$(HOME)/.gconf") == 0) ||
00136                     (strstr(line, "~/.gconf") == 0) ||
00137                     (strstr(line, data_filename)))
00138             {
00139                 found_user_dir = TRUE;
00140                 break;
00141             }
00142         }
00143         g_strfreev(lines);
00144     }
00145 
00146     output = g_fopen(path_filename, "a");
00147     if (output == NULL)
00148     {
00149         *error = g_error_new (G_FILE_ERROR,
00150                               g_file_error_from_errno(errno),
00151                               "Error opening file %s for writing.",
00152                               path_filename);
00153         g_free(path_filename);
00154         g_free(data_filename);
00155         return FALSE;
00156     }
00157 
00158     fprintf(output, "\n######## The following lines were added by GnuCash. ########\n");
00159     if (!found_user_dir)
00160         fprintf(output, PATH_STRING1);
00161     gconfdir = gnc_path_get_gconfdir (TRUE);
00162     fprintf(output, PATH_STRING2, gconfdir);
00163     g_free (gconfdir);
00164     fprintf(output,   "############## End of lines added by GnuCash. ##############\n");
00165     if (fclose(output) != 0)
00166     {
00167         *error = g_error_new (G_FILE_ERROR,
00168                               g_file_error_from_errno(errno),
00169                               "Error closing file %s.",
00170                               path_filename);
00171         g_free(path_filename);
00172         g_free(data_filename);
00173         return  FALSE;
00174     }
00175 
00176     g_free(path_filename);
00177     g_free(data_filename);
00178     return TRUE;
00179 }
00180 
00181 
00196 static gboolean
00197 druid_gconf_install_keys (GError **error)
00198 {
00199     return g_spawn_command_line_sync(SCRIPT_NAME, NULL, NULL, NULL, error);
00200 }
00201 
00202 /********************
00203  * Common Callbacks
00204  ********************/
00205 
00206 
00211 gboolean
00212 druid_gconf_delete_event (GtkWidget *window,
00213                           GdkEvent *event,
00214                           gpointer user_data)
00215 {
00216     gtk_widget_destroy(GTK_WIDGET(window));
00217     exit(40);
00218 }
00219 
00220 
00224 void
00225 druid_gconf_cancel (GnomeDruid *druid,
00226                     gpointer user_data)
00227 {
00228     GtkWidget *window;
00229 
00230     window = gnc_glade_lookup_widget(GTK_WIDGET(druid), "GConf Install Druid");
00231     gtk_widget_destroy(GTK_WIDGET(window));
00232     exit(41);
00233 }
00234 
00235 
00236 /********************
00237  * Choose Page
00238  ********************/
00239 
00240 
00245 void
00246 druid_gconf_choose_page_prepare (GnomeDruidPage *druidpage,
00247                                  GnomeDruid *druid,
00248                                  gpointer user_data)
00249 {
00250 }
00251 
00252 
00258 gboolean
00259 druid_gconf_choose_page_next (GnomeDruidPage *druidpage,
00260                               GnomeDruid *druid,
00261                               gpointer user_data)
00262 {
00263     GtkWidget *page, *button;
00264 
00265     button = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "update_path");
00266     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
00267     {
00268         page = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "update_page");
00269         g_object_set_data(G_OBJECT(druid), HOW, GINT_TO_POINTER(HOW_UPDATE));
00270     }
00271     else
00272     {
00273         page = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "install_page");
00274         g_object_set_data(G_OBJECT(druid), HOW, GINT_TO_POINTER(HOW_INSTALL));
00275     }
00276 
00277     gnome_druid_set_page(druid, GNOME_DRUID_PAGE(page));
00278     return TRUE;
00279 }
00280 
00281 
00282 /********************
00283  * Update Page
00284  ********************/
00285 
00286 
00292 void
00293 druid_gconf_update_page_prepare (GnomeDruidPage *druidpage,
00294                                  GnomeDruid *druid,
00295                                  gpointer user_data)
00296 {
00297     GtkTextBuffer *textbuffer;
00298     GtkWidget *textview;
00299     gchar *msg;
00300     gchar *gconfdir = gnc_path_get_gconfdir (TRUE);
00301 
00302     textview = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "update_text");
00303     textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
00304     msg = g_strdup_printf(PATH_STRING1 PATH_STRING2, gconfdir);
00305     gtk_text_buffer_set_text(textbuffer, msg, -1);
00306     g_free (gconfdir);
00307 }
00308 
00309 
00314 gboolean
00315 druid_gconf_update_page_next (GnomeDruidPage *druidpage,
00316                               GnomeDruid *druid,
00317                               gpointer user_data)
00318 {
00319     GtkWidget *page, *button1, *button2;
00320 
00321     button1 = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "program1");
00322     button2 = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "user1");
00323     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button1)))
00324     {
00325         g_object_set_data(G_OBJECT(druid), WHO_DOES, GINT_TO_POINTER(WHO_GNUCASH));
00326     }
00327     else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button2)))
00328     {
00329         g_object_set_data(G_OBJECT(druid), WHO_DOES, GINT_TO_POINTER(WHO_USER));
00330     }
00331     else
00332     {
00333         g_object_set_data(G_OBJECT(druid), WHO_DOES, GINT_TO_POINTER(WHO_ALREADY_DONE));
00334     }
00335 
00336     page = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "finish_page");
00337     gnome_druid_set_page(druid, GNOME_DRUID_PAGE(page));
00338     return TRUE;
00339 }
00340 
00341 /********************
00342  * Install Page
00343  ********************/
00344 
00349 void
00350 druid_gconf_install_page_prepare (GnomeDruidPage *druidpage,
00351                                   GnomeDruid *druid,
00352                                   gpointer user_data)
00353 {
00354     GtkTextBuffer *textbuffer;
00355     GtkWidget *textview;
00356 
00357     textview = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "install_text");
00358     textbuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
00359     gtk_text_buffer_set_text(textbuffer, SCRIPT_NAME, -1);
00360 }
00361 
00362 
00367 gboolean
00368 druid_gconf_install_page_next (GnomeDruidPage *druidpage,
00369                                GnomeDruid *druid,
00370                                gpointer user_data)
00371 {
00372     GtkWidget *page, *button1, *button2;
00373 
00374     button1 = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "program2");
00375     button2 = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "user2");
00376     if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button1)))
00377     {
00378         g_object_set_data(G_OBJECT(druid), WHO_DOES, GINT_TO_POINTER(WHO_GNUCASH));
00379     }
00380     else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button2)))
00381     {
00382         g_object_set_data(G_OBJECT(druid), WHO_DOES, GINT_TO_POINTER(WHO_USER));
00383     }
00384     else
00385     {
00386         g_object_set_data(G_OBJECT(druid), WHO_DOES, GINT_TO_POINTER(WHO_ALREADY_DONE));
00387     }
00388 
00389     page = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "finish_page");
00390     gnome_druid_set_page(druid, GNOME_DRUID_PAGE(page));
00391     return TRUE;
00392 }
00393 
00394 
00398 gboolean
00399 druid_gconf_install_page_back (GnomeDruidPage *druidpage,
00400                                GnomeDruid *druid,
00401                                gpointer user_data)
00402 {
00403     GtkWidget *page;
00404 
00405     page = gnc_glade_lookup_widget(GTK_WIDGET(druidpage), "choose_page");
00406     gnome_druid_set_page(druid, GNOME_DRUID_PAGE(page));
00407     return TRUE;
00408 }
00409 
00410 
00411 /********************
00412  * Finish Page
00413  ********************/
00414 
00415 
00420 void
00421 druid_gconf_finish_page_prepare (GnomeDruidPage *druidpage,
00422                                  GnomeDruid *druid,
00423                                  gpointer user_data)
00424 {
00425     gint who, how;
00426     gchar *text;
00427     const gchar *pgm_path =
00428         _("When you click Apply, GnuCash will modify your ~/.gconf.path file "
00429           "and restart the gconf backend.");
00430     const gchar *pgm_install =
00431         _("When you click Apply, GnuCash will install the gconf data into your "
00432           "local ~/.gconf file and restart the gconf backend.  The %s script "
00433           "must be found in your search path for this to work correctly.");
00434     const gchar *user_path =
00435         _("You have chosen to correct the problem by yourself.  When you click "
00436           "Apply, GnuCash will exit.  Please correct the problem and restart "
00437           "the gconf backend with the command 'gconftool-2 --shutdown' before "
00438           "restarting GnuCash.  If you have not already done so, you can click "
00439           "the Back button and copy the necessary text from the dialog.");
00440     const gchar *user_install =
00441         _("You have chosen to correct the problem by yourself.  When you "
00442           "click Apply, GnuCash will exit.  Please run the %s script which "
00443           "will install the configuration data and restart the gconf backend.");
00444     const gchar *user_did =
00445         _("You have already corrected the problem and restarted the gconf "
00446           "backend with the command 'gconftool-2 --shutdown'.  When you click "
00447           "Apply, GnuCash will continue loading.");
00448 
00449     who = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(druid), WHO_DOES));
00450     switch (who)
00451     {
00452     case WHO_ALREADY_DONE:
00453         gnome_druid_page_edge_set_text(GNOME_DRUID_PAGE_EDGE(druidpage),
00454                                        user_did);
00455         break;
00456 
00457     case WHO_USER:
00458         how = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(druid), HOW));
00459         if (how == HOW_INSTALL)
00460         {
00461             text = g_strdup_printf(user_install, SCRIPT_NAME);
00462             gnome_druid_page_edge_set_text(GNOME_DRUID_PAGE_EDGE(druidpage), text);
00463             g_free(text);
00464         }
00465         else
00466         {
00467             gnome_druid_page_edge_set_text(GNOME_DRUID_PAGE_EDGE(druidpage),
00468                                            user_path);
00469         }
00470         break;
00471 
00472     default:
00473         how = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(druid), HOW));
00474         if (how == HOW_INSTALL)
00475         {
00476             text = g_strdup_printf(pgm_install, SCRIPT_NAME);
00477             gnome_druid_page_edge_set_text(GNOME_DRUID_PAGE_EDGE(druidpage), text);
00478             g_free(text);
00479         }
00480         else
00481         {
00482             gnome_druid_page_edge_set_text(GNOME_DRUID_PAGE_EDGE(druidpage),
00483                                            pgm_path);
00484         }
00485         break;
00486     }
00487 }
00488 
00489 
00494 gboolean
00495 druid_gconf_finish_page_back (GnomeDruidPage *druidpage,
00496                               GnomeDruid *druid,
00497                               gpointer user_data)
00498 {
00499     return druid_gconf_choose_page_next(druidpage, druid, user_data);
00500 }
00501 
00502 
00508 void
00509 druid_gconf_finish_page_finish (GnomeDruidPage *druidpage,
00510                                 GnomeDruid *druid,
00511                                 gpointer user_data)
00512 {
00513     GtkWidget *window;
00514     gint value, value2;
00515     GError *error = NULL;
00516     gboolean keep_going = TRUE;
00517 
00518     /* What to do... what to do... */
00519     value = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(druid), WHO_DOES));
00520     switch (value)
00521     {
00522     case WHO_ALREADY_DONE:
00523         break;
00524 
00525     case WHO_USER:
00526         keep_going = FALSE;
00527         break;
00528 
00529     default:
00530         value2 = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(druid), HOW));
00531         switch (value2)
00532         {
00533         case HOW_INSTALL:
00534             if (!druid_gconf_install_keys(&error))
00535             {
00536                 keep_going = FALSE;
00537                 gnc_error_dialog(NULL, "%s", error->message);
00538                 g_error_free(error);
00539             }
00540             break;
00541 
00542         default:
00543             if (!druid_gconf_update_path(&error))
00544             {
00545                 keep_going = FALSE;
00546                 gnc_error_dialog(NULL, "%s", error->message);
00547                 g_error_free(error);
00548             }
00549             break;
00550         }
00551         break;
00552     }
00553 
00554     window = gnc_glade_lookup_widget(GTK_WIDGET(druid), "GConf Install Druid");
00555     gtk_widget_destroy(GTK_WIDGET(window));
00556     if (keep_going)
00557     {
00558         gtk_main_quit();
00559     }
00560     else
00561     {
00562         exit(42);
00563     }
00564 }
00565 
00566 
00567 /********************
00568  * Druid Creation
00569  ********************/
00570 
00571 static void
00572 druid_gconf_fix_textview_color (GtkWidget *window)
00573 {
00574     GdkColor *color;
00575     GtkWidget *widget;
00576     gint i;
00577     const gchar *names[] =
00578     {
00579         "textview1",
00580         "textview2",
00581         "textview3",
00582         "textview4",
00583         "textview5",
00584         "textview6",
00585         NULL
00586     };
00587 
00588     widget = gnc_glade_lookup_widget(window, "choose_page");
00589     color = &GNOME_DRUID_PAGE_STANDARD(widget)->contents_background;
00590 
00591     for (i = 0; names[i]; i++)
00592     {
00593         widget = gnc_glade_lookup_widget(widget, names[i]);
00594         gtk_widget_modify_base(widget, GTK_STATE_INSENSITIVE, color);
00595     }
00596 }
00597 
00606 static void
00607 gnc_gnome_install_gconf_schemas (void)
00608 {
00609     GladeXML *xml;
00610     GtkWidget *window;
00611     GError *error = NULL;
00612 
00613     xml = gnc_glade_xml_new ("druid-gconf-setup.glade", "GConf Install Druid");
00614     glade_xml_signal_autoconnect_full(xml, gnc_glade_autoconnect_full_func, NULL);
00615     window = glade_xml_get_widget (xml, "GConf Install Druid");
00616     druid_gconf_fix_textview_color(window);
00617     gtk_widget_show_all(window);
00618 
00619     /* This won't return until the dialog is finished */
00620     gtk_main();
00621 
00622     /* Kill the backend daemon. When it restarts it will find our changes */
00623     if (!g_spawn_command_line_sync("gconftool-2 --shutdown", NULL, NULL,
00624                                    NULL, &error))
00625     {
00626         gnc_warning_dialog(NULL, "%s", error->message);
00627         g_error_free(error);
00628     }
00629 }
00630 
00631 
00632 /*  This routine checks to see if GnuCash's gconf schemas are visible
00633  *  to the user.  The schemas typically should be visible, as rpm and
00634  *  deb installs will put the schemas in the default system location.
00635  *  For things like network installs or developers, this function will
00636  *  present a warning dialog that asks the user whether to setup
00637  *  gconf, continue without the schemas, or quit.  If the user chooses
00638  *  to set up the schemas, this function will invoke a druid to walk
00639  *  the user through making the schemas visible.
00640  */
00641 void
00642 druid_gconf_install_check_schemas (void)
00643 {
00644     GladeXML *xml;
00645     GtkWidget *dialog;
00646     gboolean done = FALSE;
00647     gint response;
00648 
00649     if (gnc_gconf_schemas_found())
00650     {
00651         gnc_gconf_unset_dir(GCONF_WARNINGS_TEMP, NULL);
00652         return;
00653     }
00654 
00655 #ifdef G_OS_WIN32
00656     {
00657         /* automatically update the search path on windows */
00658         GError *error = NULL;
00659         if (!druid_gconf_update_path (&error))
00660         {
00661             gnc_error_dialog (NULL, error->message);
00662             g_error_free (error);
00663             exit(42);
00664         }
00665         else
00666         {
00667             if (!g_spawn_command_line_sync("gconftool-2 --shutdown", NULL, NULL,
00668                                            NULL, &error))
00669             {
00670                 gnc_warning_dialog(NULL, error->message);
00671                 g_error_free(error);
00672             }
00673             return;
00674         }
00675     }
00676 #endif /* G_OS_WIN32 */
00677 
00678     xml = gnc_glade_xml_new ("druid-gconf-setup.glade", "GConf Query");
00679     if (!xml)
00680     {
00681         gnc_error_dialog(NULL, "The glade UI files were not found. Your installation is incomplete and cannot be run.");
00682         exit(-1); /* quit immediately */
00683     }
00684     g_assert(xml);
00685     dialog = glade_xml_get_widget (xml, "GConf Query");
00686     g_assert(dialog);
00687     do
00688     {
00689         response = gtk_dialog_run(GTK_DIALOG(dialog));
00690 
00691         switch (response)
00692         {
00693         case GTK_RESPONSE_CANCEL:
00694         default:
00695             gnc_shutdown(42);
00696             /* never returns */
00697 
00698         case GTK_RESPONSE_NO:
00699             /* User wants to run without setting up gconf */
00700             done = TRUE;
00701             break;
00702 
00703         case GTK_RESPONSE_ACCEPT:
00704             gtk_widget_hide(dialog);
00705             gnc_gnome_install_gconf_schemas();
00706             done = TRUE;
00707             break;
00708 
00709         case GTK_RESPONSE_HELP:
00710             gnc_gnome_help(HF_HELP, HL_GCONF);
00711             break;
00712         }
00713     }
00714     while (!done);
00715 
00716     gtk_widget_destroy(dialog);
00717 }
00718 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines