Added new runtime option --new-win.
[lxde/pcmanfm.git] / src / pcmanfm.c
CommitLineData
b6e3c554
HJYP
1/*
2 * pcmanfm.c
d3451adb 3 *
7b7d923f 4 * Copyright 2009 - 2010 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
984ccbd9 5 * Copyright 2012 Andriy Grytsenko (LStranger) <andrej@rep.kiev.ua>
d3451adb 6 *
b6e3c554
HJYP
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
d3451adb 11 *
b6e3c554
HJYP
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
d3451adb 16 *
b6e3c554
HJYP
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
21 */
22
08e70fea 23#ifdef HAVE_CONFIG_H
b6e3c554 24#include <config.h>
08e70fea
HJYP
25#endif
26
b6e3c554 27#include <gtk/gtk.h>
1098cfe7 28#include <gdk/gdkx.h>
b6e3c554 29#include <stdio.h>
f8f2bfad
HJYP
30#include <glib/gi18n.h>
31
32#include <stdlib.h>
33#include <string.h>
34/* socket is used to keep single instance */
35#include <sys/types.h>
f8f2bfad
HJYP
36#include <signal.h>
37#include <unistd.h> /* for getcwd */
b6e3c554 38
20c0bc9a 39#include <libfm/fm-gtk.h>
4d55886c 40#include "app-config.h"
b6e3c554 41#include "main-win.h"
8505a8ef 42#include "desktop.h"
71f82759 43#include "volume-manager.h"
c5fccf1d 44#include "pref.h"
df6826e0 45#include "pcmanfm.h"
cc448d7f 46#include "single-inst.h"
f8f2bfad 47
94435bc3 48static int signal_pipe[2] = {-1, -1};
2873a0f8 49static gboolean daemon_mode = FALSE;
3d141640 50static guint save_config_idle = 0;
f8f2bfad 51
f8f2bfad 52static char** files_to_open = NULL;
cc448d7f 53static int n_files_to_open = 0;
f970e846 54static char* profile = NULL;
f8f2bfad
HJYP
55static gboolean no_desktop = FALSE;
56static gboolean show_desktop = FALSE;
57static gboolean desktop_off = FALSE;
58static gboolean desktop_running = FALSE;
107cdb22 59/* static gboolean new_tab = FALSE; */
f8f2bfad
HJYP
60static int show_pref = 0;
61static gboolean desktop_pref = FALSE;
62static char* set_wallpaper = NULL;
107cdb22 63static char* wallpaper_mode = NULL;
2873a0f8 64static gboolean new_win = FALSE;
f8f2bfad 65static gboolean find_files = FALSE;
5b890032 66static char* ipc_cwd = NULL;
fbce80c9 67/* static char* window_role = NULL; */
f8f2bfad
HJYP
68
69static int n_pcmanfm_ref = 0;
70
71static GOptionEntry opt_entries[] =
8505a8ef 72{
cda6259f 73 /* options only acceptable by first pcmanfm instance. These options are not passed through IPC */
a48f9fc8 74 { "profile", 'p', 0, G_OPTION_ARG_STRING, &profile, N_("Name of configuration profile"), "<profile name>" },
cda6259f
HJYP
75 { "daemon-mode", 'd', 0, G_OPTION_ARG_NONE, &daemon_mode, N_("Run PCManFM as a daemon"), NULL },
76 { "no-desktop", '\0', 0, G_OPTION_ARG_NONE, &no_desktop, N_("No function. Just to be compatible with nautilus"), NULL },
77
78 /* options that are acceptable for every instance of pcmanfm and will be passed through IPC. */
f8f2bfad
HJYP
79 { "desktop", '\0', 0, G_OPTION_ARG_NONE, &show_desktop, N_("Launch desktop manager"), NULL },
80 { "desktop-off", '\0', 0, G_OPTION_ARG_NONE, &desktop_off, N_("Turn off desktop manager if it's running"), NULL },
f8f2bfad 81 { "desktop-pref", '\0', 0, G_OPTION_ARG_NONE, &desktop_pref, N_("Open desktop preference dialog"), NULL },
f970e846 82 { "set-wallpaper", 'w', 0, G_OPTION_ARG_FILENAME, &set_wallpaper, N_("Set desktop wallpaper"), N_("<image file>") },
8ee5c1ac 83 /* don't translate list of modes in description, please */
107cdb22 84 { "wallpaper-mode", '\0', 0, G_OPTION_ARG_STRING, &wallpaper_mode, N_("Set mode of desktop wallpaper. <mode>=(color|stretch|fit|center|tile)"), N_("<mode>") },
a48f9fc8 85 { "show-pref", '\0', 0, G_OPTION_ARG_INT, &show_pref, N_("Open preference dialog. 'n' is number of the page you want to show (1, 2, 3...)."), "n" },
2873a0f8 86 { "new-win", 'n', 0, G_OPTION_ARG_NONE, &new_win, N_("Open new window"), NULL },
1d071341 87 /* { "find-files", 'f', 0, G_OPTION_ARG_NONE, &find_files, N_("Open Find Files utility"), NULL }, */
fbce80c9 88 /* { "role", '\0', 0, G_OPTION_ARG_STRING, &window_role, N_("Window role for usage by window manager", N_("ROLE") }, */
f8f2bfad
HJYP
89 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files_to_open, NULL, N_("[FILE1, FILE2,...]")},
90 { NULL }
91};
92
107cdb22
HJYP
93static const char* valid_wallpaper_modes[] = {"color", "stretch", "fit", "center", "tile"};
94
f8f2bfad 95static gboolean pcmanfm_run();
b6e3c554 96
94435bc3
HJYP
97/* it's not safe to call gtk+ functions in unix signal handler
98 * since the process is interrupted here and the state of gtk+ is unpredictable. */
99static void unix_signal_handler(int sig_num)
100{
101 /* postpond the signal handling by using a pipe */
3aebc679
AG
102 if (write(signal_pipe[1], &sig_num, sizeof(sig_num)) != sizeof(sig_num)) {
103 g_critical("cannot bounce the signal, stop");
104 exit(2);
105 }
94435bc3
HJYP
106}
107
108static gboolean on_unix_signal(GIOChannel* ch, GIOCondition cond, gpointer user_data)
109{
110 int sig_num;
2ddc3882
AG
111 GIOStatus status;
112 gsize got;
113
114 while(1)
94435bc3 115 {
2ddc3882
AG
116 status = g_io_channel_read_chars(ch, (gchar*)&sig_num, sizeof(sig_num),
117 &got, NULL);
118 if(status == G_IO_STATUS_AGAIN) /* we read all the pipe */
119 {
120 g_debug("got G_IO_STATUS_AGAIN");
121 return TRUE;
122 }
123 if(status != G_IO_STATUS_NORMAL || got != sizeof(sig_num)) /* broken pipe */
124 {
125 g_debug("signal pipe is broken");
126 gtk_main_quit();
127 return FALSE;
128 }
129 g_debug("got signal %d from pipe", sig_num);
130 switch(sig_num)
131 {
132 case SIGTERM:
133 default:
134 gtk_main_quit();
135 return FALSE;
136 }
94435bc3
HJYP
137 }
138 return TRUE;
139}
140
1098cfe7 141static void single_inst_cb(const char* cwd, int screen_num)
cc448d7f 142{
cda6259f
HJYP
143 g_free(ipc_cwd);
144 ipc_cwd = g_strdup(cwd);
145
146 if(files_to_open)
cc448d7f 147 {
cda6259f 148 int i;
6033d802 149 n_files_to_open = g_strv_length(files_to_open);
cda6259f
HJYP
150 /* canonicalize filename if needed. */
151 for(i = 0; i < n_files_to_open; ++i)
cc448d7f 152 {
cda6259f
HJYP
153 char* file = files_to_open[i];
154 char* scheme = g_uri_parse_scheme(file);
6033d802 155 g_debug("file: %s", file);
cda6259f 156 if(scheme) /* a valid URI */
cc448d7f 157 {
cda6259f
HJYP
158 /* FIXME: should we canonicalize URIs? and how about file:///? */
159 g_free(scheme);
160 }
161 else /* a file path */
162 {
163 files_to_open[i] = fm_canonicalize_filename(file, cwd);
164 g_free(file);
cc448d7f 165 }
cc448d7f 166 }
cc448d7f 167 }
cda6259f 168 pcmanfm_run();
2873a0f8 169 //window_role = NULL; /* reset it for clients callbacks */
cc448d7f
HJYP
170}
171
b6e3c554
HJYP
172int main(int argc, char** argv)
173{
4d55886c 174 FmConfig* config;
f8f2bfad 175 GError* err = NULL;
21df32aa 176 SingleInstData inst;
cda6259f 177
f8f2bfad
HJYP
178#ifdef ENABLE_NLS
179 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
180 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
181 textdomain ( GETTEXT_PACKAGE );
182#endif
183
184 /* initialize GTK+ and parse the command line arguments */
185 if(G_UNLIKELY(!gtk_init_with_args(&argc, &argv, "", opt_entries, GETTEXT_PACKAGE, &err)))
186 {
187 g_printf("%s\n", err->message);
c5fccf1d 188 g_error_free(err);
f8f2bfad
HJYP
189 return 1;
190 }
191
cc448d7f 192 /* ensure that there is only one instance of pcmanfm. */
21df32aa
AG
193 inst.prog_name = "pcmanfm";
194 inst.cb = single_inst_cb;
195 inst.opt_entries = opt_entries + 3;
196 inst.screen_num = gdk_x11_get_default_screen();
197 switch(single_inst_init(&inst))
cc448d7f
HJYP
198 {
199 case SINGLE_INST_CLIENT: /* we're not the first instance. */
21df32aa 200 single_inst_finalize(&inst);
cc448d7f
HJYP
201 gdk_notify_startup_complete();
202 return 0;
203 case SINGLE_INST_ERROR: /* error happened. */
21df32aa 204 single_inst_finalize(&inst);
cc448d7f 205 return 1;
3aebc679 206 case SINGLE_INST_SERVER: ; /* FIXME */
cc448d7f 207 }
f8f2bfad 208
94435bc3
HJYP
209 if(pipe(signal_pipe) == 0)
210 {
211 GIOChannel* ch = g_io_channel_unix_new(signal_pipe[0]);
212 g_io_add_watch(ch, G_IO_IN|G_IO_PRI, (GIOFunc)on_unix_signal, NULL);
213 g_io_channel_unref(ch);
214
215 /* intercept signals */
ae2947d9
HJYP
216 // signal( SIGPIPE, SIG_IGN );
217 signal( SIGHUP, unix_signal_handler );
94435bc3 218 signal( SIGTERM, unix_signal_handler );
2ddc3882 219 signal( SIGINT, unix_signal_handler );
94435bc3 220 }
b6e3c554 221
f970e846 222 config = fm_app_config_new(); /* this automatically load libfm config file. */
17d12cc9 223
f970e846 224 /* load pcmanfm-specific config file */
17d12cc9 225 fm_app_config_load_from_profile(FM_APP_CONFIG(config), profile);
f970e846 226
94435bc3 227 fm_gtk_init(config);
f8f2bfad
HJYP
228 /* the main part */
229 if(pcmanfm_run())
230 {
2873a0f8 231 //window_role = NULL; /* reset it for clients callbacks */
71f82759 232 fm_volume_manager_init();
94435bc3 233 gtk_main();
2ddc3882 234 /* g_debug("main loop ended"); */
f8f2bfad
HJYP
235 if(desktop_running)
236 fm_desktop_manager_finalize();
3d141640
HJYP
237
238 pcmanfm_save_config(TRUE);
239 if(save_config_idle)
240 {
241 g_source_remove(save_config_idle);
242 save_config_idle = 0;
243 }
71f82759 244 fm_volume_manager_finalize();
f8f2bfad 245 }
cc448d7f 246
21df32aa 247 single_inst_finalize(&inst);
f8f2bfad 248 fm_gtk_finalize();
ae2947d9 249
f8f2bfad 250 g_object_unref(config);
94435bc3 251 return 0;
f8f2bfad
HJYP
252}
253
ee12f37e
HJYP
254static FmJobErrorAction on_file_info_job_error(FmFileInfoJob* job, GError* err, FmJobErrorSeverity severity, gpointer user_data)
255{
16a17d6b 256 if(err->domain == G_IO_ERROR)
ee12f37e 257 {
16a17d6b
HJYP
258 if(err->code == G_IO_ERROR_NOT_MOUNTED)
259 {
260 if(fm_mount_path(NULL, fm_file_info_job_get_current(job), TRUE))
261 return FM_JOB_RETRY;
262 }
263 else if(err->code == G_IO_ERROR_FAILED_HANDLED)
264 return FM_JOB_CONTINUE;
ee12f37e 265 }
5089e13a 266 fm_show_error(NULL, NULL, err->message);
ee12f37e
HJYP
267 return FM_JOB_CONTINUE;
268}
f8f2bfad
HJYP
269
270gboolean pcmanfm_run()
271{
2873a0f8 272 FmMainWin *win;
f8f2bfad 273 gboolean ret = TRUE;
f8f2bfad 274
f8f2bfad
HJYP
275 if(!files_to_open)
276 {
277 /* Launch desktop manager */
278 if(show_desktop)
279 {
280 if(!desktop_running)
281 {
282 fm_desktop_manager_init();
283 desktop_running = TRUE;
284 }
19abb8bf 285 show_desktop = FALSE;
f8f2bfad
HJYP
286 return TRUE;
287 }
288 else if(desktop_off)
289 {
290 if(desktop_running)
291 {
292 desktop_running = FALSE;
293 fm_desktop_manager_finalize();
294 }
19abb8bf 295 desktop_off = FALSE;
f8f2bfad
HJYP
296 return FALSE;
297 }
298 else if(show_pref > 0)
299 {
7d78ab22 300 /* FIXME: pass screen number from client */
c65ba572 301 fm_edit_preference(GTK_WINDOW(fm_desktop_get(0, 0)), show_pref - 1);
c5fccf1d 302 show_pref = 0;
f8f2bfad
HJYP
303 return TRUE;
304 }
305 else if(desktop_pref)
306 {
7d78ab22 307 /* FIXME: pass screen number from client */
c65ba572 308 fm_desktop_preference(NULL, GTK_WINDOW(fm_desktop_get(0, 0)));
19abb8bf 309 desktop_pref = FALSE;
f8f2bfad
HJYP
310 return TRUE;
311 }
107cdb22 312 else
f8f2bfad 313 {
107cdb22
HJYP
314 gboolean need_to_exit = (wallpaper_mode || set_wallpaper);
315 gboolean wallpaper_changed = FALSE;
316 if(set_wallpaper) /* a new wallpaper is assigned */
317 {
318 /* g_debug("\'%s\'", set_wallpaper); */
319 /* Make sure this is a support image file. */
320 if(gdk_pixbuf_get_file_info(set_wallpaper, NULL, NULL))
321 {
322 if(app_config->wallpaper)
323 g_free(app_config->wallpaper);
e0854cdb 324 app_config->wallpaper = g_strdup(set_wallpaper);
107cdb22
HJYP
325 if(! wallpaper_mode) /* if wallpaper mode is not specified */
326 {
327 /* do not use solid color mode; otherwise wallpaper won't be shown. */
328 if(app_config->wallpaper_mode == FM_WP_COLOR)
329 app_config->wallpaper_mode = FM_WP_FIT;
330 }
331 wallpaper_changed = TRUE;
332 }
333 }
334
335 if(wallpaper_mode)
336 {
6f0f1950 337 guint i = 0;
107cdb22
HJYP
338 for(i = 0; i < G_N_ELEMENTS(valid_wallpaper_modes); ++i)
339 {
340 if(strcmp(valid_wallpaper_modes[i], wallpaper_mode) == 0)
341 {
342 if(i != app_config->wallpaper_mode)
343 {
344 app_config->wallpaper_mode = i;
345 wallpaper_changed = TRUE;
346 }
347 break;
348 }
349 }
350 g_free(wallpaper_mode);
351 wallpaper_mode = NULL;
352 }
353
354 if(wallpaper_changed)
f8f2bfad 355 {
4b3e1fe9 356 fm_config_emit_changed(FM_CONFIG(app_config), "wallpaper");
17d12cc9 357 fm_app_config_save_profile(app_config, profile);
f8f2bfad 358 }
107cdb22
HJYP
359
360 if(need_to_exit)
361 return FALSE;
f8f2bfad
HJYP
362 }
363 }
364
365 if(G_UNLIKELY(find_files))
366 {
367 /* FIXME: find files */
368 }
369 else
370 {
df6826e0
HJYP
371 if(files_to_open)
372 {
373 char** filename;
6a9e415d 374 /* FIXME: use fm_launch_paths_simple() so don't use job */
3aebc679 375 FmFileInfoJob* job = fm_file_info_job_new(NULL, 0);
cacf261a 376 FmPath* cwd = NULL;
d951adb8 377 GList* infos;
df6826e0
HJYP
378 for(filename=files_to_open; *filename; ++filename)
379 {
cacf261a 380 FmPath* path;
9197e75e
HJYP
381 if( **filename == '/') /* absolute path */
382 path = fm_path_new_for_path(*filename);
383 else if(strstr(*filename, ":/") ) /* URI */
384 path = fm_path_new_for_uri(*filename);
0d8dce1a
HJYP
385 else if( strcmp(*filename, "~") == 0 ) /* special case for home dir */
386 {
387 path = fm_path_get_home();
2873a0f8
AG
388 win = fm_main_win_add_win(NULL, path);
389// if(window_role)
390// gtk_window_set_role(GTK_WINDOW(win), window_role);
0d8dce1a
HJYP
391 continue;
392 }
cacf261a
HJYP
393 else /* basename */
394 {
395 if(G_UNLIKELY(!cwd))
396 {
397 /* FIXME: This won't work if those filenames are passed via IPC since the receiving process has different cwd. */
9197e75e 398 /* FIXME: should we use ipc_cwd here? */
cacf261a 399 char* cwd_str = g_get_current_dir();
653af525 400 cwd = fm_path_new_for_str(cwd_str);
cacf261a
HJYP
401 g_free(cwd_str);
402 }
403 path = fm_path_new_relative(cwd, *filename);
404 }
3aebc679 405 fm_file_info_job_add(job, path);
df6826e0
HJYP
406 fm_path_unref(path);
407 }
cacf261a
HJYP
408 if(cwd)
409 fm_path_unref(cwd);
6a9e415d 410 /* FIXME: use fm_launch_paths_simple() instead */
ee12f37e 411 g_signal_connect(job, "error", G_CALLBACK(on_file_info_job_error), NULL);
3aebc679 412 fm_job_run_sync_with_mainloop(FM_JOB(job));
5a89e062 413 infos = fm_file_info_list_peek_head_link(job->file_infos);
d951adb8 414 fm_launch_files_simple(NULL, NULL, infos, pcmanfm_open_folder, NULL);
df6826e0 415 g_object_unref(job);
cacf261a 416 ret = (n_pcmanfm_ref >= 1); /* if there is opened window, return true to run the main loop. */
2b46b521
HJYP
417
418 g_strfreev(files_to_open);
419 files_to_open = NULL;
df6826e0
HJYP
420 }
421 else
f8f2bfad 422 {
bc88a8dc
HJYP
423 static gboolean first_run = TRUE;
424 if(first_run && daemon_mode)
3d141640 425 {
bc88a8dc
HJYP
426 /* If the function is called the first time and we're in daemon mode,
427 * don't open any folder.
428 * Checking if pcmanfm_run() is called the first time is needed to fix
429 * #3397444 - pcmanfm dont show window in daemon mode if i call 'pcmanfm' */
430 }
431 else
432 {
433 /* If we're not in daemon mode, or pcmanfm_run() is called because another
434 * instance send signal to us, open cwd by default. */
3d141640
HJYP
435 FmPath* path;
436 char* cwd = ipc_cwd ? ipc_cwd : g_get_current_dir();
437 path = fm_path_new_for_path(cwd);
2873a0f8
AG
438 win = fm_main_win_add_win(NULL, path);
439// if(window_role)
440// gtk_window_set_role(GTK_WINDOW(win), window_role);
3d141640
HJYP
441 fm_path_unref(path);
442 g_free(cwd);
443 ipc_cwd = NULL;
444 }
bc88a8dc 445 first_run = FALSE;
f8f2bfad
HJYP
446 }
447 }
448 return ret;
449}
450
451/* After opening any window/dialog/tool, this should be called. */
452void pcmanfm_ref()
453{
454 ++n_pcmanfm_ref;
455 /* g_debug("ref: %d", n_pcmanfm_ref); */
456}
457
458/* After closing any window/dialog/tool, this should be called.
459 * If the last window is closed and we are not a deamon, pcmanfm will quit.
460 */
461void pcmanfm_unref()
462{
463 --n_pcmanfm_ref;
464 /* g_debug("unref: %d, daemon_mode=%d, desktop_running=%d", n_pcmanfm_ref, daemon_mode, desktop_running); */
465 if( 0 == n_pcmanfm_ref && !daemon_mode && !desktop_running )
466 gtk_main_quit();
b6e3c554 467}
df6826e0 468
7d78ab22
AG
469static void move_window_to_desktop(FmMainWin* win, FmDesktop* desktop)
470{
471 GdkScreen* screen = gtk_widget_get_screen(GTK_WIDGET(desktop));
472 Atom atom;
473 char* atom_name = "_NET_WM_DESKTOP";
474 XClientMessageEvent xev;
475
476 gtk_window_set_screen(GTK_WINDOW(win), screen);
38459ae5 477 if(!XInternAtoms(gdk_x11_get_default_xdisplay(), &atom_name, 1, False, &atom))
7d78ab22
AG
478 {
479 /* g_debug("cannot get Atom for _NET_WM_DESKTOP"); */
480 return;
481 }
482 xev.type = ClientMessage;
38459ae5 483 xev.window = GDK_WINDOW_XID(gtk_widget_get_window(GTK_WIDGET(win)));
7d78ab22
AG
484 xev.message_type = atom;
485 xev.format = 32;
486 xev.data.l[0] = desktop->cur_desktop;
487 xev.data.l[1] = 0;
488 xev.data.l[2] = 0;
489 xev.data.l[3] = 0;
490 xev.data.l[4] = 0;
491 /* g_debug("moving window to current desktop"); */
38459ae5 492 XSendEvent(gdk_x11_get_default_xdisplay(), GDK_ROOT_WINDOW(), False,
7d78ab22
AG
493 (SubstructureNotifyMask | SubstructureRedirectMask),
494 (XEvent *) &xev);
495}
496
df6826e0
HJYP
497gboolean pcmanfm_open_folder(GAppLaunchContext* ctx, GList* folder_infos, gpointer user_data, GError** err)
498{
df6826e0 499 GList* l = folder_infos;
2873a0f8
AG
500 if(new_win)
501 {
502 FmMainWin *win = fm_main_win_add_win(NULL,
503 fm_file_info_get_path((FmFileInfo*)l->data));
504// if(window_role)
505// gtk_window_set_role(GTK_WINDOW(win), window_role);
506 new_win = FALSE;
507 l = l->next;
508 }
df6826e0
HJYP
509 for(; l; l=l->next)
510 {
511 FmFileInfo* fi = (FmFileInfo*)l->data;
0b951729 512 fm_main_win_open_in_last_active(fm_file_info_get_path(fi));
df6826e0 513 }
7d78ab22
AG
514 if(user_data && FM_IS_DESKTOP(user_data))
515 move_window_to_desktop(fm_main_win_get_last_active(), user_data);
df6826e0
HJYP
516 return TRUE;
517}
dcecce78 518
3d141640 519static gboolean on_save_config_idle(gpointer user_data)
dcecce78 520{
3d141640
HJYP
521 pcmanfm_save_config(TRUE);
522 save_config_idle = 0;
523 return FALSE;
524}
525
526void pcmanfm_save_config(gboolean immediate)
527{
528 if(immediate)
529 {
530 fm_config_save(fm_config, NULL);
531 fm_app_config_save_profile(app_config, profile);
532 }
533 else
534 {
535 /* install an idle handler to save the config file. */
f04f9105
AG
536 if( 0 == save_config_idle)
537 save_config_idle = g_idle_add_full(G_PRIORITY_LOW, (GSourceFunc)on_save_config_idle, NULL, NULL);
3d141640 538 }
dcecce78 539}
c56a211a
HJYP
540
541void pcmanfm_open_folder_in_terminal(GtkWindow* parent, FmPath* dir)
542{
543 GAppInfo* app;
c56a211a
HJYP
544 char** argv;
545 int argc;
546 if(!fm_config->terminal)
547 {
5089e13a 548 fm_show_error(parent, NULL, _("Terminal emulator is not set."));
c56a211a
HJYP
549 fm_edit_preference(parent, PREF_ADVANCED);
550 return;
551 }
552 if(!g_shell_parse_argv(fm_config->terminal, &argc, &argv, NULL))
553 return;
554 app = g_app_info_create_from_commandline(argv[0], NULL, 0, NULL);
555 g_strfreev(argv);
556 if(app)
557 {
558 GError* err = NULL;
3aebc679 559 GdkAppLaunchContext* ctx = gdk_app_launch_context_new();
c56a211a 560 char* cwd_str;
1dfc8e4c 561 char* old_cwd = g_get_current_dir();
c56a211a
HJYP
562
563 if(fm_path_is_native(dir))
564 cwd_str = fm_path_to_str(dir);
565 else
566 {
567 GFile* gf = fm_path_to_gfile(dir);
568 cwd_str = g_file_get_path(gf);
569 g_object_unref(gf);
570 }
3aebc679
AG
571 gdk_app_launch_context_set_screen(ctx, parent ? gtk_widget_get_screen(GTK_WIDGET(parent)) : gdk_screen_get_default());
572 gdk_app_launch_context_set_timestamp(ctx, gtk_get_current_event_time());
c56a211a
HJYP
573 g_chdir(cwd_str); /* FIXME: currently we don't have better way for this. maybe a wrapper script? */
574 g_free(cwd_str);
1dfc8e4c 575
3aebc679 576 if(!g_app_info_launch(app, NULL, G_APP_LAUNCH_CONTEXT(ctx), &err))
c56a211a 577 {
5089e13a 578 fm_show_error(parent, NULL, err->message);
c56a211a
HJYP
579 g_error_free(err);
580 }
581 g_object_unref(ctx);
582 g_object_unref(app);
1dfc8e4c
HJYP
583
584 /* switch back to old cwd and fix #3114626 - PCManFM 0.9.9 Umount partitions problem */
585 g_chdir(old_cwd); /* This is really dirty, but we don't have better solution now. */
586 g_free(old_cwd);
c56a211a
HJYP
587 }
588}
7cb06c27 589
92f8ca49
HJYP
590char* pcmanfm_get_profile_dir(gboolean create)
591{
592 char* dir = g_build_filename(g_get_user_config_dir(), "pcmanfm", profile ? profile : "default", NULL);
593 if(create)
594 g_mkdir_with_parents(dir, 0700);
595 return dir;
596}