Don't save config if nothing was changed.
[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>
824fed5b 5 * Copyright 2012-2014 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;
0a9dfa8a 50static gboolean first_run = TRUE;
3d141640 51static guint save_config_idle = 0;
f8f2bfad 52
f8f2bfad 53static char** files_to_open = NULL;
cc448d7f 54static int n_files_to_open = 0;
f970e846 55static char* profile = NULL;
f8f2bfad
HJYP
56static gboolean no_desktop = FALSE;
57static gboolean show_desktop = FALSE;
58static gboolean desktop_off = FALSE;
59static gboolean desktop_running = FALSE;
a906053c 60static gboolean one_screen = FALSE;
107cdb22 61/* static gboolean new_tab = FALSE; */
bc99152c 62static gint show_pref = -1;
f8f2bfad
HJYP
63static gboolean desktop_pref = FALSE;
64static char* set_wallpaper = NULL;
107cdb22 65static char* wallpaper_mode = NULL;
2873a0f8 66static gboolean new_win = FALSE;
541a1b08 67#if FM_CHECK_VERSION(1, 0, 2)
f8f2bfad 68static gboolean find_files = FALSE;
541a1b08 69#endif
5b890032 70static char* ipc_cwd = NULL;
024eff7c 71static char* window_role = NULL;
f8f2bfad
HJYP
72
73static int n_pcmanfm_ref = 0;
74
75static GOptionEntry opt_entries[] =
8505a8ef 76{
cda6259f 77 /* options only acceptable by first pcmanfm instance. These options are not passed through IPC */
86ef20e0 78 { "profile", 'p', 0, G_OPTION_ARG_STRING, &profile, N_("Name of configuration profile"), N_("PROFILE") },
cda6259f
HJYP
79 { "daemon-mode", 'd', 0, G_OPTION_ARG_NONE, &daemon_mode, N_("Run PCManFM as a daemon"), NULL },
80 { "no-desktop", '\0', 0, G_OPTION_ARG_NONE, &no_desktop, N_("No function. Just to be compatible with nautilus"), NULL },
81
82 /* options that are acceptable for every instance of pcmanfm and will be passed through IPC. */
f8f2bfad
HJYP
83 { "desktop", '\0', 0, G_OPTION_ARG_NONE, &show_desktop, N_("Launch desktop manager"), NULL },
84 { "desktop-off", '\0', 0, G_OPTION_ARG_NONE, &desktop_off, N_("Turn off desktop manager if it's running"), NULL },
f8f2bfad 85 { "desktop-pref", '\0', 0, G_OPTION_ARG_NONE, &desktop_pref, N_("Open desktop preference dialog"), NULL },
a906053c 86 { "one-screen", '\0', 0, G_OPTION_ARG_NONE, &one_screen, N_("Use --desktop option only for one screen"), NULL },
86ef20e0 87 { "set-wallpaper", 'w', 0, G_OPTION_ARG_FILENAME, &set_wallpaper, N_("Set desktop wallpaper from image FILE"), N_("FILE") },
8ee5c1ac 88 /* don't translate list of modes in description, please */
be584ddf 89 { "wallpaper-mode", '\0', 0, G_OPTION_ARG_STRING, &wallpaper_mode, N_("Set mode of desktop wallpaper. MODE=(color|stretch|fit|crop|center|tile|screen)"), N_("MODE") },
86ef20e0 90 { "show-pref", '\0', 0, G_OPTION_ARG_INT, &show_pref, N_("Open Preferences dialog on the page N"), N_("N") },
2873a0f8 91 { "new-win", 'n', 0, G_OPTION_ARG_NONE, &new_win, N_("Open new window"), NULL },
541a1b08
AG
92#if FM_CHECK_VERSION(1, 0, 2)
93 { "find-files", 'f', 0, G_OPTION_ARG_NONE, &find_files, N_("Open a Find Files window"), NULL },
94#endif
024eff7c 95 { "role", '\0', 0, G_OPTION_ARG_STRING, &window_role, N_("Window role for usage by window manager"), N_("ROLE") },
f8f2bfad
HJYP
96 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files_to_open, NULL, N_("[FILE1, FILE2,...]")},
97 { NULL }
98};
99
a906053c 100static gboolean pcmanfm_run(gint screen_num);
b6e3c554 101
94435bc3
HJYP
102/* it's not safe to call gtk+ functions in unix signal handler
103 * since the process is interrupted here and the state of gtk+ is unpredictable. */
104static void unix_signal_handler(int sig_num)
105{
106 /* postpond the signal handling by using a pipe */
3aebc679
AG
107 if (write(signal_pipe[1], &sig_num, sizeof(sig_num)) != sizeof(sig_num)) {
108 g_critical("cannot bounce the signal, stop");
109 exit(2);
110 }
94435bc3
HJYP
111}
112
113static gboolean on_unix_signal(GIOChannel* ch, GIOCondition cond, gpointer user_data)
114{
115 int sig_num;
2ddc3882
AG
116 GIOStatus status;
117 gsize got;
118
119 while(1)
94435bc3 120 {
2ddc3882
AG
121 status = g_io_channel_read_chars(ch, (gchar*)&sig_num, sizeof(sig_num),
122 &got, NULL);
123 if(status == G_IO_STATUS_AGAIN) /* we read all the pipe */
124 {
125 g_debug("got G_IO_STATUS_AGAIN");
126 return TRUE;
127 }
128 if(status != G_IO_STATUS_NORMAL || got != sizeof(sig_num)) /* broken pipe */
129 {
130 g_debug("signal pipe is broken");
131 gtk_main_quit();
132 return FALSE;
133 }
134 g_debug("got signal %d from pipe", sig_num);
135 switch(sig_num)
136 {
137 case SIGTERM:
138 default:
139 gtk_main_quit();
140 return FALSE;
141 }
94435bc3
HJYP
142 }
143 return TRUE;
144}
145
1098cfe7 146static void single_inst_cb(const char* cwd, int screen_num)
cc448d7f 147{
cda6259f
HJYP
148 g_free(ipc_cwd);
149 ipc_cwd = g_strdup(cwd);
150
151 if(files_to_open)
cc448d7f 152 {
cda6259f 153 int i;
6033d802 154 n_files_to_open = g_strv_length(files_to_open);
cda6259f
HJYP
155 /* canonicalize filename if needed. */
156 for(i = 0; i < n_files_to_open; ++i)
cc448d7f 157 {
cda6259f
HJYP
158 char* file = files_to_open[i];
159 char* scheme = g_uri_parse_scheme(file);
6033d802 160 g_debug("file: %s", file);
cda6259f 161 if(scheme) /* a valid URI */
cc448d7f 162 {
cda6259f
HJYP
163 g_free(scheme);
164 }
165 else /* a file path */
166 {
167 files_to_open[i] = fm_canonicalize_filename(file, cwd);
168 g_free(file);
cc448d7f 169 }
cc448d7f 170 }
cc448d7f 171 }
a906053c 172 pcmanfm_run(screen_num);
024eff7c 173 window_role = NULL; /* reset it for clients callbacks */
cc448d7f
HJYP
174}
175
b462b7e0
AG
176#if FM_CHECK_VERSION(1, 2, 0)
177/* ---- statusbar plugins support ---- */
178FM_MODULE_DEFINE_TYPE(tab_page_status, FmTabPageStatusInit, 1)
179
180GList *_tab_page_modules = NULL;
181
182static gboolean fm_module_callback_tab_page_status(const char *name, gpointer init, int ver)
183{
184 /* add module callbacks into own data list */
185 if (((FmTabPageStatusInit*)init)->sel_message == NULL)
186 return FALSE;
4d1b07ca
AG
187 if (((FmTabPageStatusInit*)init)->init && !((FmTabPageStatusInit*)init)->init())
188 return FALSE;
b462b7e0
AG
189 _tab_page_modules = g_list_append(_tab_page_modules, init);
190 return TRUE;
191}
192#endif
193
e3da4ec1
AG
194static void on_config_changed(FmAppConfig *cfg, gpointer _unused)
195{
196 pcmanfm_save_config(FALSE);
197}
198
b6e3c554
HJYP
199int main(int argc, char** argv)
200{
4d55886c 201 FmConfig* config;
f8f2bfad 202 GError* err = NULL;
21df32aa 203 SingleInstData inst;
4d1b07ca
AG
204#if FM_CHECK_VERSION(1, 2, 0)
205 GList *l;
206#endif
cda6259f 207
f8f2bfad
HJYP
208#ifdef ENABLE_NLS
209 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
210 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
211 textdomain ( GETTEXT_PACKAGE );
212#endif
213
214 /* initialize GTK+ and parse the command line arguments */
f17ed8cf 215 if(G_UNLIKELY(!gtk_init_with_args(&argc, &argv, " ", opt_entries, GETTEXT_PACKAGE, &err)))
f8f2bfad
HJYP
216 {
217 g_printf("%s\n", err->message);
c5fccf1d 218 g_error_free(err);
f8f2bfad
HJYP
219 return 1;
220 }
221
cc448d7f 222 /* ensure that there is only one instance of pcmanfm. */
21df32aa
AG
223 inst.prog_name = "pcmanfm";
224 inst.cb = single_inst_cb;
225 inst.opt_entries = opt_entries + 3;
226 inst.screen_num = gdk_x11_get_default_screen();
227 switch(single_inst_init(&inst))
cc448d7f
HJYP
228 {
229 case SINGLE_INST_CLIENT: /* we're not the first instance. */
21df32aa 230 single_inst_finalize(&inst);
cc448d7f
HJYP
231 gdk_notify_startup_complete();
232 return 0;
233 case SINGLE_INST_ERROR: /* error happened. */
21df32aa 234 single_inst_finalize(&inst);
cc448d7f 235 return 1;
3aebc679 236 case SINGLE_INST_SERVER: ; /* FIXME */
cc448d7f 237 }
f8f2bfad 238
94435bc3
HJYP
239 if(pipe(signal_pipe) == 0)
240 {
241 GIOChannel* ch = g_io_channel_unix_new(signal_pipe[0]);
242 g_io_add_watch(ch, G_IO_IN|G_IO_PRI, (GIOFunc)on_unix_signal, NULL);
243 g_io_channel_unref(ch);
244
245 /* intercept signals */
ae2947d9
HJYP
246 // signal( SIGPIPE, SIG_IGN );
247 signal( SIGHUP, unix_signal_handler );
94435bc3 248 signal( SIGTERM, unix_signal_handler );
2ddc3882 249 signal( SIGINT, unix_signal_handler );
94435bc3 250 }
b6e3c554 251
f970e846 252 config = fm_app_config_new(); /* this automatically load libfm config file. */
17d12cc9 253
759cc12c
AG
254 fm_gtk_init(config);
255
f372cd67
AG
256#if FM_CHECK_VERSION(1, 2, 0)
257 /* register our modules */
258 fm_modules_add_directory(PACKAGE_MODULES_DIR);
259 fm_module_register_tab_page_status();
260#endif
261
4e2edfe5
AG
262#if FM_CHECK_VERSION(1, 0, 2)
263# if !FM_CHECK_VERSION(1, 2, 0)
264 /* the sort_by value isn't loaded with LibFM 1.1.x so we need a workaround */
759cc12c 265 /* a little trick to initialize FmFolderModel class */
4e2edfe5 266 gpointer null_model = fm_folder_model_new(NULL, FALSE);
759cc12c 267 g_object_unref(null_model);
4e2edfe5
AG
268# endif
269#endif
759cc12c 270
f970e846 271 /* load pcmanfm-specific config file */
17d12cc9 272 fm_app_config_load_from_profile(FM_APP_CONFIG(config), profile);
e3da4ec1 273 g_signal_connect(config, "changed::saved_search", G_CALLBACK(on_config_changed), NULL);
f970e846 274
f8f2bfad 275 /* the main part */
a906053c 276 if(pcmanfm_run(gdk_screen_get_number(gdk_screen_get_default())))
f8f2bfad 277 {
0a9dfa8a 278 first_run = FALSE;
024eff7c 279 window_role = NULL; /* reset it for clients callbacks */
71f82759 280 fm_volume_manager_init();
35b3f192
AG
281#if !GTK_CHECK_VERSION(3, 6, 0)
282 GDK_THREADS_ENTER();
283#endif
94435bc3 284 gtk_main();
2ddc3882 285 /* g_debug("main loop ended"); */
35b3f192
AG
286#if !GTK_CHECK_VERSION(3, 6, 0)
287 GDK_THREADS_LEAVE();
288#endif
3d141640 289
3d141640
HJYP
290 if(save_config_idle)
291 {
21edaff8 292 pcmanfm_save_config(TRUE);
3d141640
HJYP
293 g_source_remove(save_config_idle);
294 save_config_idle = 0;
295 }
71f82759 296 fm_volume_manager_finalize();
f8f2bfad 297 }
cc448d7f 298
b462b7e0 299#if FM_CHECK_VERSION(1, 2, 0)
4d1b07ca
AG
300 for (l = _tab_page_modules; l; l = l->next)
301 if (((FmTabPageStatusInit*)l->data)->finalize)
302 ((FmTabPageStatusInit*)l->data)->finalize();
f83ae8af 303 fm_module_unregister_type("tab_page_status");
b462b7e0
AG
304 g_list_free(_tab_page_modules);
305 _tab_page_modules = NULL;
306#endif
307
21df32aa 308 single_inst_finalize(&inst);
f8f2bfad 309 fm_gtk_finalize();
ae2947d9 310
f8f2bfad 311 g_object_unref(config);
94435bc3 312 return 0;
f8f2bfad
HJYP
313}
314
a906053c 315gboolean pcmanfm_run(gint screen_num)
f8f2bfad 316{
541a1b08 317 FmMainWin *win = NULL;
f8f2bfad 318 gboolean ret = TRUE;
f8f2bfad 319
f8f2bfad
HJYP
320 if(!files_to_open)
321 {
93a7ee1f
AG
322 /* FIXME: use screen number from client and pointer position */
323 FmDesktop *desktop = fm_desktop_get(0, 0);
324
f8f2bfad
HJYP
325 /* Launch desktop manager */
326 if(show_desktop)
327 {
328 if(!desktop_running)
329 {
a906053c 330 fm_desktop_manager_init(one_screen ? screen_num : -1);
f8f2bfad 331 desktop_running = TRUE;
a906053c 332 one_screen = FALSE;
f8f2bfad 333 }
19abb8bf 334 show_desktop = FALSE;
f8f2bfad
HJYP
335 return TRUE;
336 }
337 else if(desktop_off)
338 {
339 if(desktop_running)
340 {
341 desktop_running = FALSE;
342 fm_desktop_manager_finalize();
343 }
19abb8bf 344 desktop_off = FALSE;
f8f2bfad
HJYP
345 return FALSE;
346 }
93a7ee1f
AG
347 else if(desktop == NULL)
348 /* ignore desktop-oriented commands if no desktop support */;
f8f2bfad
HJYP
349 else if(show_pref > 0)
350 {
93a7ee1f 351 fm_edit_preference(GTK_WINDOW(desktop), show_pref - 1);
bc99152c 352 show_pref = -1;
f8f2bfad
HJYP
353 return TRUE;
354 }
355 else if(desktop_pref)
356 {
93a7ee1f 357 fm_desktop_preference(NULL, desktop);
19abb8bf 358 desktop_pref = FALSE;
f8f2bfad
HJYP
359 return TRUE;
360 }
93a7ee1f 361 else if(wallpaper_mode || set_wallpaper)
f8f2bfad 362 {
107cdb22
HJYP
363 gboolean wallpaper_changed = FALSE;
364 if(set_wallpaper) /* a new wallpaper is assigned */
365 {
366 /* g_debug("\'%s\'", set_wallpaper); */
367 /* Make sure this is a support image file. */
368 if(gdk_pixbuf_get_file_info(set_wallpaper, NULL, NULL))
369 {
93a7ee1f
AG
370 if(desktop->conf.wallpaper)
371 g_free(desktop->conf.wallpaper);
372 desktop->conf.wallpaper = set_wallpaper;
107cdb22
HJYP
373 if(! wallpaper_mode) /* if wallpaper mode is not specified */
374 {
375 /* do not use solid color mode; otherwise wallpaper won't be shown. */
93a7ee1f
AG
376 if(desktop->conf.wallpaper_mode == FM_WP_COLOR)
377 desktop->conf.wallpaper_mode = FM_WP_FIT;
107cdb22
HJYP
378 }
379 wallpaper_changed = TRUE;
380 }
1009f571
AG
381 else
382 g_free(set_wallpaper);
383 set_wallpaper = NULL;
107cdb22
HJYP
384 }
385
386 if(wallpaper_mode)
387 {
824fed5b
AG
388 FmWallpaperMode mode = fm_app_wallpaper_get_mode_by_name(wallpaper_mode);
389
390 if (mode != (FmWallpaperMode)-1
391 && mode != desktop->conf.wallpaper_mode)
107cdb22 392 {
824fed5b
AG
393 desktop->conf.wallpaper_mode = mode;
394 wallpaper_changed = TRUE;
107cdb22
HJYP
395 }
396 g_free(wallpaper_mode);
397 wallpaper_mode = NULL;
398 }
399
400 if(wallpaper_changed)
93a7ee1f 401 fm_desktop_wallpaper_changed(desktop);
107cdb22 402
93a7ee1f 403 return FALSE;
f8f2bfad
HJYP
404 }
405 }
406
4ba1c42e
AG
407 if(files_to_open)
408 {
409 char** filename;
410 FmPath* cwd = NULL;
411 GList* paths = NULL;
412 for(filename=files_to_open; *filename; ++filename)
df6826e0 413 {
4ba1c42e
AG
414 FmPath* path;
415 if( **filename == '/') /* absolute path */
416 path = fm_path_new_for_path(*filename);
417 else if(strstr(*filename, ":/") ) /* URI */
418 path = fm_path_new_for_uri(*filename);
419 else if( strcmp(*filename, "~") == 0 ) /* special case for home dir */
df6826e0 420 {
4ba1c42e
AG
421 path = fm_path_get_home();
422 win = fm_main_win_add_win(NULL, path);
423 if(new_win && window_role)
424 gtk_window_set_role(GTK_WINDOW(win), window_role);
425 continue;
426 }
427 else /* basename */
428 {
429 if(G_UNLIKELY(!cwd))
cacf261a 430 {
4ba1c42e
AG
431 char* cwd_str = ipc_cwd ? ipc_cwd : g_get_current_dir();
432 cwd = fm_path_new_for_str(cwd_str);
433 g_free(cwd_str);
cacf261a 434 }
4ba1c42e 435 path = fm_path_new_relative(cwd, *filename);
df6826e0 436 }
4ba1c42e 437 paths = g_list_append(paths, path);
df6826e0 438 }
4ba1c42e
AG
439 if(cwd)
440 fm_path_unref(cwd);
441 fm_launch_paths_simple(NULL, NULL, paths, pcmanfm_open_folder, NULL);
442 g_list_foreach(paths, (GFunc)fm_path_unref, NULL);
443 g_list_free(paths);
444 ret = (n_pcmanfm_ref >= 1); /* if there is opened window, return true to run the main loop. */
445
446 g_strfreev(files_to_open);
447 files_to_open = NULL;
448 }
449 else
450 {
451 if(first_run && daemon_mode)
f8f2bfad 452 {
4ba1c42e
AG
453 /* If the function is called the first time and we're in daemon mode,
454 * don't open any folder.
455 * Checking if pcmanfm_run() is called the first time is needed to fix
456 * #3397444 - pcmanfm dont show window in daemon mode if i call 'pcmanfm' */
457 pcmanfm_ref();
f8f2bfad 458 }
b8c3393f 459#if FM_CHECK_VERSION(1, 0, 2)
4ba1c42e 460 else if (G_LIKELY(!find_files || n_pcmanfm_ref < 1))
b8c3393f
AG
461#else
462 else
463#endif
4ba1c42e
AG
464 {
465 /* If we're not in daemon mode, or pcmanfm_run() is called because another
466 * instance send signal to us, open cwd by default. */
467 FmPath* path;
468 char* cwd = ipc_cwd ? ipc_cwd : g_get_current_dir();
469 path = fm_path_new_for_path(cwd);
470 win = fm_main_win_add_win(NULL, path);
471 if(new_win && window_role)
472 gtk_window_set_role(GTK_WINDOW(win), window_role);
473 fm_path_unref(path);
474 g_free(cwd);
475 ipc_cwd = NULL;
476 }
477 }
541a1b08
AG
478
479#if FM_CHECK_VERSION(1, 0, 2)
480 /* we got a reference at this point so we can open a search dialog */
481 if (ret && find_files)
482 fm_launch_search_simple(GTK_WINDOW(win), NULL, NULL,
483 pcmanfm_open_folder, NULL);
484 find_files = FALSE;
485#endif
f8f2bfad
HJYP
486 return ret;
487}
488
489/* After opening any window/dialog/tool, this should be called. */
490void pcmanfm_ref()
491{
492 ++n_pcmanfm_ref;
493 /* g_debug("ref: %d", n_pcmanfm_ref); */
494}
495
496/* After closing any window/dialog/tool, this should be called.
497 * If the last window is closed and we are not a deamon, pcmanfm will quit.
498 */
499void pcmanfm_unref()
500{
501 --n_pcmanfm_ref;
502 /* g_debug("unref: %d, daemon_mode=%d, desktop_running=%d", n_pcmanfm_ref, daemon_mode, desktop_running); */
0a9dfa8a 503 if( 0 == n_pcmanfm_ref )
f8f2bfad 504 gtk_main_quit();
b6e3c554 505}
df6826e0 506
7d78ab22
AG
507static void move_window_to_desktop(FmMainWin* win, FmDesktop* desktop)
508{
509 GdkScreen* screen = gtk_widget_get_screen(GTK_WIDGET(desktop));
510 Atom atom;
511 char* atom_name = "_NET_WM_DESKTOP";
512 XClientMessageEvent xev;
513
514 gtk_window_set_screen(GTK_WINDOW(win), screen);
38459ae5 515 if(!XInternAtoms(gdk_x11_get_default_xdisplay(), &atom_name, 1, False, &atom))
7d78ab22
AG
516 {
517 /* g_debug("cannot get Atom for _NET_WM_DESKTOP"); */
518 return;
519 }
520 xev.type = ClientMessage;
38459ae5 521 xev.window = GDK_WINDOW_XID(gtk_widget_get_window(GTK_WIDGET(win)));
7d78ab22
AG
522 xev.message_type = atom;
523 xev.format = 32;
524 xev.data.l[0] = desktop->cur_desktop;
525 xev.data.l[1] = 0;
526 xev.data.l[2] = 0;
527 xev.data.l[3] = 0;
528 xev.data.l[4] = 0;
529 /* g_debug("moving window to current desktop"); */
38459ae5 530 XSendEvent(gdk_x11_get_default_xdisplay(), GDK_ROOT_WINDOW(), False,
7d78ab22
AG
531 (SubstructureNotifyMask | SubstructureRedirectMask),
532 (XEvent *) &xev);
533}
534
df6826e0
HJYP
535gboolean pcmanfm_open_folder(GAppLaunchContext* ctx, GList* folder_infos, gpointer user_data, GError** err)
536{
df6826e0 537 GList* l = folder_infos;
55c0a7f6
AG
538 gboolean use_new_win = new_win;
539
540 /* for desktop folder open it in new win if set in config */
541 if (!use_new_win && user_data && FM_IS_DESKTOP(user_data))
542 use_new_win = app_config->desktop_folder_new_win;
543 if(use_new_win)
2873a0f8
AG
544 {
545 FmMainWin *win = fm_main_win_add_win(NULL,
546 fm_file_info_get_path((FmFileInfo*)l->data));
024eff7c
AG
547 if(window_role)
548 gtk_window_set_role(GTK_WINDOW(win), window_role);
2873a0f8
AG
549 new_win = FALSE;
550 l = l->next;
551 }
df6826e0
HJYP
552 for(; l; l=l->next)
553 {
554 FmFileInfo* fi = (FmFileInfo*)l->data;
0b951729 555 fm_main_win_open_in_last_active(fm_file_info_get_path(fi));
df6826e0 556 }
7d78ab22
AG
557 if(user_data && FM_IS_DESKTOP(user_data))
558 move_window_to_desktop(fm_main_win_get_last_active(), user_data);
df6826e0
HJYP
559 return TRUE;
560}
dcecce78 561
3d141640 562static gboolean on_save_config_idle(gpointer user_data)
dcecce78 563{
3d141640
HJYP
564 pcmanfm_save_config(TRUE);
565 save_config_idle = 0;
566 return FALSE;
567}
568
569void pcmanfm_save_config(gboolean immediate)
570{
571 if(immediate)
572 {
573 fm_config_save(fm_config, NULL);
574 fm_app_config_save_profile(app_config, profile);
575 }
576 else
577 {
578 /* install an idle handler to save the config file. */
f04f9105 579 if( 0 == save_config_idle)
c3fd847a 580 save_config_idle = gdk_threads_add_idle_full(G_PRIORITY_LOW, (GSourceFunc)on_save_config_idle, NULL, NULL);
3d141640 581 }
dcecce78 582}
c56a211a
HJYP
583
584void pcmanfm_open_folder_in_terminal(GtkWindow* parent, FmPath* dir)
585{
586 GAppInfo* app;
c56a211a
HJYP
587 char** argv;
588 int argc;
589 if(!fm_config->terminal)
590 {
5089e13a 591 fm_show_error(parent, NULL, _("Terminal emulator is not set."));
c56a211a
HJYP
592 fm_edit_preference(parent, PREF_ADVANCED);
593 return;
594 }
595 if(!g_shell_parse_argv(fm_config->terminal, &argc, &argv, NULL))
596 return;
597 app = g_app_info_create_from_commandline(argv[0], NULL, 0, NULL);
598 g_strfreev(argv);
599 if(app)
600 {
601 GError* err = NULL;
3aebc679 602 GdkAppLaunchContext* ctx = gdk_app_launch_context_new();
c56a211a 603 char* cwd_str;
1dfc8e4c 604 char* old_cwd = g_get_current_dir();
c56a211a
HJYP
605
606 if(fm_path_is_native(dir))
607 cwd_str = fm_path_to_str(dir);
608 else
609 {
610 GFile* gf = fm_path_to_gfile(dir);
611 cwd_str = g_file_get_path(gf);
612 g_object_unref(gf);
613 }
3aebc679
AG
614 gdk_app_launch_context_set_screen(ctx, parent ? gtk_widget_get_screen(GTK_WIDGET(parent)) : gdk_screen_get_default());
615 gdk_app_launch_context_set_timestamp(ctx, gtk_get_current_event_time());
c56a211a
HJYP
616 g_chdir(cwd_str); /* FIXME: currently we don't have better way for this. maybe a wrapper script? */
617 g_free(cwd_str);
1dfc8e4c 618
3aebc679 619 if(!g_app_info_launch(app, NULL, G_APP_LAUNCH_CONTEXT(ctx), &err))
c56a211a 620 {
5089e13a 621 fm_show_error(parent, NULL, err->message);
c56a211a
HJYP
622 g_error_free(err);
623 }
624 g_object_unref(ctx);
625 g_object_unref(app);
1dfc8e4c
HJYP
626
627 /* switch back to old cwd and fix #3114626 - PCManFM 0.9.9 Umount partitions problem */
628 g_chdir(old_cwd); /* This is really dirty, but we don't have better solution now. */
629 g_free(old_cwd);
c56a211a
HJYP
630 }
631}
7cb06c27 632
92f8ca49
HJYP
633char* pcmanfm_get_profile_dir(gboolean create)
634{
635 char* dir = g_build_filename(g_get_user_config_dir(), "pcmanfm", profile ? profile : "default", NULL);
636 if(create)
637 g_mkdir_with_parents(dir, 0700);
638 return dir;
639}