src/cmd/fsexam/src/fsexam-pref.c
changeset 147 8c4ef02c14b8
equal deleted inserted replaced
146:841e634f8d60 147:8c4ef02c14b8
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
       
    24  * Use is subject to license terms.
       
    25  */
       
    26 
       
    27 #ifdef HAVE_CONFIG_H
       
    28 #include <config.h>
       
    29 #endif
       
    30 
       
    31 #include <glib.h>
       
    32 #include <gtk/gtk.h>
       
    33 #include <gconf/gconf-client.h>
       
    34 
       
    35 #include "fsexam.h"
       
    36 #include "fsexam-header.h"
       
    37 
       
    38 void cb_toolbar_style (GConfClient *, guint, GConfEntry *, gpointer);
       
    39 
       
    40 static GSList *
       
    41 remove_duplicated_encoding_name (GSList *list, GSList *standard)
       
    42 {
       
    43     GSList *result = list;
       
    44 
       
    45     while (list != NULL) {
       
    46         GSList *second = standard;
       
    47         
       
    48         while (second != NULL) {
       
    49             if (encoding2id (list->data) == encoding2id (second->data))
       
    50                 break;
       
    51             second = g_slist_next (second);
       
    52         }
       
    53 
       
    54         if (second != NULL) {       /* duplicated, remove it */
       
    55             GSList *next = NULL;
       
    56 
       
    57             next = g_slist_next (list);
       
    58             result = g_slist_delete_link (result, list);
       
    59             list = next;
       
    60         } else {
       
    61             list = g_slist_next (list);
       
    62         }
       
    63     }
       
    64 
       
    65     return result;
       
    66 }
       
    67 
       
    68 FSEXAM_pref *
       
    69 fsexam_pref_init (gboolean cmd_mode)
       
    70 {
       
    71     FSEXAM_pref *pref = NULL;
       
    72     GConfClient *gconf_client = NULL;
       
    73 
       
    74     pref = g_new0 (FSEXAM_pref, 1);
       
    75 
       
    76     gconf_client = pref->gconf_client = gconf_client_get_default ();
       
    77     pref->hist_len = gconf_client_get_int (gconf_client, HISTLEN, NULL);
       
    78 
       
    79     if (cmd_mode){
       
    80         pref->auto_detect = FALSE;
       
    81         pref->hidden = FALSE;
       
    82         pref->auto_conversion= FALSE;
       
    83         pref->recursive = FALSE;
       
    84         pref->remote = FALSE;
       
    85         pref->follow = FALSE;
       
    86         pref->no_check_symlink_content = FALSE;
       
    87         pref->use_log = FALSE;
       
    88         pref->log_file = NULL;
       
    89         pref->suffix_list = NULL;
       
    90         pref->special = 0;
       
    91     }else{
       
    92 		gconf_client_notify_add (gconf_client,
       
    93 				TOOLBAR_STYLE,
       
    94 				cb_toolbar_style,
       
    95 				NULL, NULL, NULL);
       
    96 
       
    97         pref->auto_detect = gconf_client_get_bool (gconf_client,
       
    98                                     AUTODETECTMODE,
       
    99                                     NULL);
       
   100         pref->hidden = gconf_client_get_bool (gconf_client,
       
   101                                     HIDDENMODE,
       
   102                                     NULL);
       
   103         pref->auto_conversion= gconf_client_get_bool (gconf_client,
       
   104                                     INTERACTIVEMODE,
       
   105                                     NULL);
       
   106         pref->recursive = gconf_client_get_bool (gconf_client,
       
   107                                     RECURSIVEMODE,
       
   108                                     NULL);
       
   109         pref->remote = gconf_client_get_bool (gconf_client,
       
   110                                     REMOTEMODE,
       
   111                                     NULL);
       
   112         pref->follow = gconf_client_get_bool (gconf_client,
       
   113                                     FOLLOWMODE,
       
   114                                     NULL);
       
   115         pref->use_log = gconf_client_get_bool (gconf_client,
       
   116                                     USELOG,
       
   117                                     NULL);
       
   118         pref->no_check_symlink_content = gconf_client_get_bool (gconf_client,
       
   119                                     CHECKSYMLINKTARGETMODE,
       
   120                                     NULL);
       
   121         pref->suffix_list = (GSList *) gconf_client_get_list (gconf_client,
       
   122                                     SUFFIXLIST,
       
   123                                     GCONF_VALUE_STRING,
       
   124                                     NULL);
       
   125         pref->log_file = gconf_client_get_string (gconf_client,
       
   126                                     LOGFILE,
       
   127                                     NULL);
       
   128         pref->special = gconf_client_get_int (gconf_client,
       
   129                                     SPECIAL,
       
   130                                     NULL);
       
   131     }
       
   132 
       
   133     pref->conv_content = FALSE;
       
   134     pref->dry_run = FALSE;
       
   135     pref->force = FALSE;
       
   136 
       
   137     pref->encode_list = NULL;
       
   138     pref->encode_name_list = NULL;
       
   139 
       
   140     return pref;
       
   141 }
       
   142 
       
   143 void
       
   144 fsexam_pref_destroy (FSEXAM_pref *pref)
       
   145 {
       
   146     if (NULL == pref)
       
   147         return;
       
   148 
       
   149     g_object_unref (pref->gconf_client);
       
   150     fsexam_encoding_destroy (pref->encode_list);
       
   151 
       
   152     fsexam_slist_free (pref->encode_name_list);
       
   153     fsexam_slist_free (pref->suffix_list);
       
   154 
       
   155     g_free (pref->log_file);
       
   156     g_free (pref);
       
   157 }
       
   158 
       
   159 /*
       
   160  *  Init encoding_list and encoding_name_list from gconf and CLI option
       
   161  */
       
   162 void
       
   163 fsexam_pref_set_encoding_list (FSEXAM_pref *pref,
       
   164                                gchar *encoding_string,
       
   165                                gboolean append,
       
   166                                gboolean prepend,
       
   167                                gboolean save)
       
   168 {
       
   169     GConfClient     *client;
       
   170 
       
   171     if (NULL == pref)
       
   172         return;
       
   173 
       
   174     client = pref->gconf_client;
       
   175 
       
   176     if (encoding_string == NULL){   /* Use GConf Encoding list only */
       
   177         pref->encode_name_list = (GSList *)gconf_client_get_list (client,
       
   178                                                     ENCODINGLIST,
       
   179                                                     GCONF_VALUE_STRING,
       
   180                                                     NULL);
       
   181         pref->encode_list = fsexam_encoding_init (pref->encode_name_list);
       
   182     }else{
       
   183         GSList      *encode_text;
       
   184 
       
   185         /* encoding list from command line arguments */
       
   186         pref->encode_name_list = encoding_string_parse (encoding_string);
       
   187 
       
   188         /* encoding list from gconf database */
       
   189         encode_text = (GSList *)gconf_client_get_list (client,
       
   190                                                        ENCODINGLIST,
       
   191                                                        GCONF_VALUE_STRING,
       
   192                                                        NULL);
       
   193         
       
   194         if (append) {
       
   195             pref->encode_name_list = remove_duplicated_encoding_name (
       
   196                     pref->encode_name_list,
       
   197                     encode_text);
       
   198             pref->encode_name_list = g_slist_concat (encode_text, 
       
   199                                                      pref->encode_name_list);
       
   200         }else if (prepend){
       
   201             pref->encode_name_list = remove_duplicated_encoding_name (
       
   202                     pref->encode_name_list,
       
   203                     encode_text);
       
   204             pref->encode_name_list = g_slist_concat (pref->encode_name_list, 
       
   205                                                      encode_text);
       
   206         }else{
       
   207             fsexam_slist_free (encode_text);    /* free encode from gconf */
       
   208         }
       
   209 
       
   210         if (save){
       
   211             gconf_client_set_list (client,
       
   212                                    ENCODINGLIST,
       
   213                                    GCONF_VALUE_STRING,
       
   214                                    pref->encode_name_list,
       
   215                                    NULL);
       
   216         }
       
   217 
       
   218         pref->encode_list = fsexam_encoding_init (pref->encode_name_list);
       
   219     }
       
   220 
       
   221     return;
       
   222 }
       
   223 
       
   224 /* 
       
   225  * Update FSEAM_pref encoding name and encoding list, save to gconf
       
   226  * if client != NULL
       
   227  *
       
   228  * slist will be used directly, so caller don't free it 
       
   229  */
       
   230 void
       
   231 fsexam_pref_update_encoding (FSEXAM_pref *pref, 
       
   232                              GSList *slist, 
       
   233                              GConfClient *client)
       
   234 {
       
   235     if (NULL == pref) {
       
   236 		fsexam_slist_free (slist);
       
   237         return;
       
   238 	}
       
   239 
       
   240     if (client != NULL) {   /* save to gconf */
       
   241         gconf_client_set_list (client,
       
   242                                ENCODINGLIST,
       
   243                                GCONF_VALUE_STRING,
       
   244                                slist,
       
   245                                NULL);
       
   246     }
       
   247 
       
   248     /* free the old data */
       
   249     fsexam_slist_free (pref->encode_name_list);
       
   250     fsexam_encoding_destroy (pref->encode_list);
       
   251    
       
   252     /* init with new data */
       
   253     pref->encode_name_list = slist;
       
   254     pref->encode_list = fsexam_encoding_init (pref->encode_name_list);
       
   255 
       
   256     return;
       
   257 }
       
   258 
       
   259 /*
       
   260  *  Save the user's preference setting into gconf database
       
   261  */
       
   262 void
       
   263 fsexam_pref_save_to_gconf (FSEXAM_pref *pref, 
       
   264                            GConfClient *client, 
       
   265                            gboolean save_encoding)
       
   266 {
       
   267     if ((NULL == pref) || (NULL == client))
       
   268         return;
       
   269 
       
   270     gconf_client_set_bool (client, AUTODETECTMODE, pref->auto_detect, NULL);
       
   271     gconf_client_set_bool (client, HIDDENMODE, pref->hidden, NULL);
       
   272     gconf_client_set_bool (client, RECURSIVEMODE, pref->recursive, NULL);
       
   273     gconf_client_set_bool (client, REMOTEMODE, pref->remote, NULL);
       
   274     gconf_client_set_bool (client, FOLLOWMODE, pref->follow, NULL);
       
   275     gconf_client_set_bool (client, USELOG, pref->use_log, NULL);
       
   276     gconf_client_set_bool (client, INTERACTIVEMODE, 
       
   277                            pref->auto_conversion, NULL);
       
   278     gconf_client_set_bool (client, CHECKSYMLINKTARGETMODE, 
       
   279                            pref->no_check_symlink_content, NULL);
       
   280 
       
   281     if (pref->log_file != NULL)
       
   282     	gconf_client_set_string (client, LOGFILE, pref->log_file, NULL);
       
   283     gconf_client_set_int (client, SPECIAL, pref->special, NULL);
       
   284     gconf_client_set_int (client, HISTLEN, pref->hist_len, NULL);
       
   285 
       
   286     if (save_encoding)
       
   287         gconf_client_set_list (client, ENCODINGLIST,
       
   288                                GCONF_VALUE_STRING, pref->encode_name_list,
       
   289                                NULL);
       
   290 
       
   291     return;
       
   292 }