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