1 /*
2  * Copyright (C) 2002-2012 Edscott Wilson Garcia
3  * EMail: edscott@users.sf.net
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program;
18  */
19 
20 
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24 
25 #include "rfm.h"
26 #include "rfm_modules.h"
27 /* the size of env_string must coincide with that of env_vars!
28 */
29 #include "primary-environment.h"
30 #include "primary-options.i"
31 static environ_t environ_v[RFM_OPTIONS + 1] = {
32     // +1 because zero does not count in enum...
33     /* general:: */
34     { "RFM_DOUBLE_CLICK_NAVIGATION", "", N_("Activate items with a double click"), NULL},
35     {"RFM_VERTICAL_TOOLBAR","",N_("Use vertical toolbar"),NULL},
36     { "RFM_USE_GTK_ICON_THEME", "", N_("Icon Theme Specification"), NULL},
37     { "RFM_DRAG_DOES_MOVE", "Yes", N_("Drag: move"), NULL},
38     { "RFM_CONTENT_FOLDER_ICONS", N_("Yes"), N_("Emblems"), NULL},
39     { "RFM_ENABLE_TIPS", N_("Yes"), N_("Enable tooltips"), NULL},
40     { "RFM_ENABLE_LABEL_TIPS", N_("Yes"), N_("Enable tooltips"), NULL},
41     {"RFM_PREVIEW_IMAGE_SIZE","",N_("Preview image size in pixels"),NULL},
42     { "RFM_FIXED_FONT_SIZE", "9", N_("The font size"), NULL},
43     { "RFM_VARIABLE_FONT_SIZE", "9", N_("The font size"), NULL},
44     { "RFM_FIXED_FONT_FAMILY", "monospace", N_("The font family"), NULL},
45     { "RFM_VARIABLE_FONT_FAMILY", "serif", N_("The font family"), NULL},
46     { "RFM_DEFAULT_ICON_SIZE", "Normal", N_("Icon size"), NULL},
47     { "TERMINAL_CMD", "roxterm", N_("Terminal Emulator"), NULL},
48     { "EDITOR", "gvim -f", N_("Text Editor"), NULL},
49     { "RFM_MAXIMUM_COMPLETION_OPTIONS", "104", N_("Maximum completion options displayed"), NULL},
50     { "RFM_LOAD_TIMEOUT", "5", N_("Maximum time (seconds) to wait for a load directory"), NULL},
51     { "RFM_MAXIMUM_DIAGNOSTIC_LINES", "1000", N_("Maximum lines in lp terminal buffer"), NULL},
52     /* desktop:: */
53     { "RFM_ENABLE_DESKTOP", "", N_("Show Desktop Grid"), NULL},
54     { "RFM_ENABLE_DESKTOP_DIAGNOSTICS", N_("Yes"), N_("Console Message Viewer"), NULL},
55     { "RFM_NAVIGATE_DESKTOP", "", N_("Navigation Window"), NULL},
56 
57     { "RFM_DESKTOP_TOP_MARGIN", "20", N_("Top Margin"), NULL},
58     { "RFM_DESKTOP_BOTTOM_MARGIN", "40", N_("Bottom Margin"), NULL},
59     { "RFM_DESKTOP_RIGHT_MARGIN", "25", N_("Right margin"), NULL},
60     { "RFM_DESKTOP_LEFT_MARGIN", "50", N_("Left Margin"), NULL},
61 
62     { "RFM_DESKTOP_DIR", "", N_("Desktop path:"), NULL},
63     { "RFM_DESKTOP_IMAGE", PREFIX"/share/images/roa153b.jpg", N_("Background image"), NULL},
64     { "RFM_DESKTOP_COLOR", "#4C1E0C", N_("Background color"), NULL},
65     { "RFM_ICONVIEW_COLOR", "#383C3F", N_("Background color"), NULL},
66     { "RFM_TRANSPARENCY", "", N_("Background transparency:"), NULL},
67 
68     { "RFM_PLUGIN_FLAGS", "0xffffffff", "plugin bitflags (lite)", NULL},
69     { "RFM_MODULE_FLAGS", "0xffffffff", "module bitflags (lite)", NULL},
70 
71     {"RFM_TOOLBAR", "", N_("Toolbar configuration")},
72     {"RFM_PASTEBOARD_SERIAL", "0", N_("Pasteboard serial control"), NULL},
73     {"RFM_BOOKMARK_SERIAL", "0", N_("Bookmark serial control"), NULL},
74 
75     ///// core options ///////////////////////////////////////////////////////
76     { "RFM_SHRED_FLAGS", "0x163", NULL, NULL},
77 #ifdef GNU_CP
78     { "RFM_LS_FLAGS", "0x2040400011", NULL, NULL},
79     { "RFM_CP_FLAGS", "0x8000891", NULL, NULL},
80     { "RFM_MV_FLAGS", "0x40b", NULL, NULL},
81     { "RFM_LN_FLAGS", "0x1113", NULL, NULL},
82     { "RFM_RM_FLAGS", "0x3aa", NULL, NULL},
83 #else
84     { "RFM_LS_FLAGS", "0x820080", NULL, NULL},
85     { "RFM_CP_FLAGS", "0xc91", NULL, NULL},
86     { "RFM_MV_FLAGS", "0x15", NULL, NULL},
87     { "RFM_LN_FLAGS", "0x791", NULL, NULL},
88     { "RFM_RM_FLAGS", "0x3ea", NULL, NULL},
89 #endif
90 
91     // core option parameters
92     { "RFM_SHRED_iterations", "3", NULL, shred_iterations},
93     { "RFM_SHRED_size", "100K", NULL, shred_size},
94 
95 #ifdef GNU_LS
96     { "RFM_LS_ignore", "", "", NULL},
97     { "RFM_LS_tabsize", "", "", NULL},
98     { "RFM_LS_blocksize", "", "", NULL},
99     { "RFM_LS_hide", "", "", NULL},
100     { "RFM_LS_width", "", "", NULL},
101     { "RFM_LS_format", NULL, NULL, ls_format},
102     { "RFM_LS_istyle", NULL, NULL, ls_istyle},
103     { "RFM_LS_qstyle", NULL, NULL, ls_qstyle},
104     { "RFM_LS_sort", NULL, NULL, ls_sort},
105     { "RFM_LS_time", NULL, NULL, ls_time},
106     { "RFM_LS_tstyle", NULL, NULL, ls_tstyle},
107 #endif
108 
109 #ifdef GNU_CP
110     { "RFM_CP_backup", NULL, NULL, cp_v_control},
111     { "RFM_CP_suffix", NULL, NULL, cp_suffix},
112     { "RFM_CP_preserve", NULL, NULL, cp_attributes},
113     { "RFM_CP_no_preserve", NULL, NULL, cp_attributes},
114     { "RFM_CP_reflink", NULL, NULL, cp_when},
115     { "RFM_CP_sparse", NULL, NULL, cp_when},
116 #endif
117 
118 #ifdef GNU_MV
119     { "RFM_MV_backup", NULL, NULL, cp_v_control},
120     { "RFM_MV_suffix", NULL, NULL, cp_suffix},
121 #endif
122 
123 #ifdef GNU_LN
124     { "RFM_LN_backup", NULL, NULL, cp_v_control},
125     { "RFM_LN_suffix", NULL, NULL, cp_suffix},
126 #endif
127 
128 #ifdef GNU_RM
129     { "RFM_RM_interactive", NULL, NULL, rm_interactive},
130 
131 #endif
132 
133 
134     {"SMB_USER", "", N_("Samba default remote user"), NULL},
135     {"SUDO_ASKPASS", PREFIX"/bin/rodent-getpass", N_("Sudo ask password program"), NULL},
136     {"SSH_ASKPASS", PREFIX"/bin/rodent-getpass", N_("Ssh ask passphrase program"), NULL},
137 
138 
139     { "VERSION_CONTROL", "existing", NULL, cp_v_control},
140     { "PWD", "", "third party stuff", NULL},
141 
142     { NULL, NULL, NULL}
143 
144 };
145 
146 static rfm_global_t *rfm_global_p = NULL;
147 
148 // This function gets static information. Better would be to get the
149 // number of currently available cores for processing.
150 static gint
get_max_threads(void)151 get_max_threads(void){
152     gint cores=MAX_PREVIEW_THREADS;
153     gchar *nproc=g_find_program_in_path("nproc");
154     if (nproc){
155         // LINUX
156         FILE *pipe = popen (nproc, "r");
157 	if(pipe) {
158             gchar buf[256];
159 	    memset(buf, 0, 256);
160             if (fgets (buf, 255, pipe) && !feof(pipe)){
161 	        errno = 0;
162                 long lcore = strtol(buf, NULL, 10);
163                 if (!errno && lcore > 0) cores = lcore;
164             }
165             pclose(pipe);
166 	}
167 	g_free (nproc);
168         return cores;
169     }
170     // No nproc?
171     gchar *sysctl=g_find_program_in_path("sysctl");
172     if (sysctl){
173         // BSD
174         gchar *cmd = g_strdup_printf("%s -a", sysctl);
175         g_free(sysctl);
176         FILE *pipe = popen (cmd, "r");
177 	if(pipe) {
178             gchar buf[256];
179 	    memset(buf, 0, 256);
180             while (fgets (buf, 255, pipe) && !feof(pipe)){
181                 if (strstr(buf,"hw.ncpu:")){
182 	            errno = 0;
183                     long lcore = strtol(buf+strlen("hw.ncpu:"), NULL, 10);
184                     // overflow not going to happen.
185                     // coverity[overflow_assign : FALSE]
186                     if (!errno && lcore > 0) cores = lcore;
187                     break;
188                 }
189             }
190             pclose(pipe);
191 	}
192 	g_free (cmd);
193     }
194 
195     return cores;
196 }
197 
rfm_global_new(void)198 rfm_global_t *rfm_global_new(void){
199     static gsize initialized = 0;
200     if (g_once_init_enter (&initialized)){
201         rfm_global_p = (rfm_global_t *) malloc (sizeof (rfm_global_t));
202         if (!rfm_global_p) g_error("malloc: %s\n", strerror(errno));
203         memset (rfm_global_p, 0, sizeof (rfm_global_t));
204         rfm_global_p->cores = get_max_threads();
205         DBG("Cores: %d\n", rfm_global_p->cores);
206 	g_once_init_leave (&initialized, 1);
207     }
208     return rfm_global_p;
209 }
210 
rfm_global(void)211 rfm_global_t *rfm_global(void){
212     return rfm_global_p;
213 }
214 
rfm_get_environ(void)215 environ_t  *rfm_get_environ(void){return environ_v;}
216 
217 /** rfm_init:
218  * Enable translations
219  *
220  */
221 
my_mkdir(gchar * g)222 static void my_mkdir(gchar *g){
223     g_mkdir_with_parents (g, 0700);
224     g_free(g);
225 }
226 
227 static GThread *gtk_thread=NULL;
rfm_set_gtk_thread(GThread * thread)228 void rfm_set_gtk_thread(GThread *thread){
229     gtk_thread = thread;
230 }
231 
rfm_get_gtk_thread(void)232 GThread *rfm_get_gtk_thread(void){
233     return gtk_thread;
234 }
235 
rfm_init(void)236 void rfm_init(void){
237 #ifdef ENABLE_NLS
238     /* this binds rfm domain: */
239     bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
240 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
241     bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
242 # endif
243 #endif
244     rfm_set_gtk_thread(g_thread_self());
245     my_mkdir(g_build_filename (DEFAULT_DESKTOP_DIR, NULL));
246     my_mkdir(g_build_filename (USER_PIXMAPS, NULL));
247     my_mkdir(g_build_filename (USER_RFM_DIR, NULL));
248     my_mkdir(g_build_filename (USER_RFM_CACHE_DIR, NULL));
249     my_mkdir(g_build_filename (USER_DBH_DIR, NULL));
250     my_mkdir(g_build_filename (USER_DBH_CACHE_DIR, NULL));
251     my_mkdir(g_build_filename (RFM_THUMBNAIL_DIR, NULL));
252 }
253