5151static void update_xdg_dir_cache (void );
5252static void schedule_user_dirs_changed (void );
5353static void desktop_dir_changed (void );
54+ static void update_xdg_user_dir (const char * type , const char * path );
5455static GFile * nemo_find_file_insensitive_next (GFile * parent , const gchar * name );
5556
5657char *
@@ -305,6 +306,32 @@ parse_xdg_dirs (const char *config_file)
305306static XdgDirEntry * cached_xdg_dirs = NULL ;
306307static GFileMonitor * cached_xdg_dirs_monitor = NULL ;
307308
309+ static void
310+ update_xdg_user_dir (const char * type , const char * path )
311+ {
312+ char * argv [5 ];
313+ int i ;
314+
315+ i = 0 ;
316+ argv [i ++ ] = (char * )"xdg-user-dirs-update" ;
317+ argv [i ++ ] = (char * )"--set" ;
318+ argv [i ++ ] = (char * )type ;
319+ argv [i ++ ] = (char * )path ;
320+ argv [i ++ ] = NULL ;
321+
322+ /* We do this sync, to avoid possible race-conditions
323+ if multiple dirs change at the same time. Its
324+ blocking the main thread, but these updates should
325+ be very rare and very fast. */
326+ g_spawn_sync (NULL ,
327+ argv , NULL ,
328+ G_SPAWN_SEARCH_PATH |
329+ G_SPAWN_STDOUT_TO_DEV_NULL |
330+ G_SPAWN_STDERR_TO_DEV_NULL ,
331+ NULL , NULL ,
332+ NULL , NULL , NULL , NULL );
333+ }
334+
308335static void
309336xdg_dir_changed (NemoFile * file ,
310337 XdgDirEntry * dir )
@@ -318,30 +345,10 @@ xdg_dir_changed (NemoFile *file,
318345 path = g_file_get_path (location );
319346
320347 if (path ) {
321- char * argv [5 ];
322- int i ;
323-
324348 g_free (dir -> path );
325349 dir -> path = path ;
326350
327- i = 0 ;
328- argv [i ++ ] = (char * )"xdg-user-dirs-update" ;
329- argv [i ++ ] = (char * )"--set" ;
330- argv [i ++ ] = dir -> type ;
331- argv [i ++ ] = dir -> path ;
332- argv [i ++ ] = NULL ;
333-
334- /* We do this sync, to avoid possible race-conditions
335- if multiple dirs change at the same time. Its
336- blocking the main thread, but these updates should
337- be very rare and very fast. */
338- g_spawn_sync (NULL ,
339- argv , NULL ,
340- G_SPAWN_SEARCH_PATH |
341- G_SPAWN_STDOUT_TO_DEV_NULL |
342- G_SPAWN_STDERR_TO_DEV_NULL ,
343- NULL , NULL ,
344- NULL , NULL , NULL , NULL );
351+ update_xdg_user_dir (dir -> type , dir -> path );
345352 g_reload_user_special_dirs_cache ();
346353 schedule_user_dirs_changed ();
347354 desktop_dir_changed ();
@@ -608,15 +615,35 @@ nemo_get_templates_directory (void)
608615}
609616
610617void
611- nemo_create_templates_directory (void )
618+ nemo_ensure_valid_templates_directory (void )
612619{
613- char * dir ;
620+ /* This is called only at points where the user would expect the Templates
621+ * directory to exist:
622+ * - Template prefs, adding a template via DND or the "New" button.
623+ * - Template prefs - the Folder icon.
624+ * - MenuBar->Go->Templates - navigating directly to the Templates dir.
625+ *
626+ * Otherwise, we're fine without it, if the user never intends to use templates.
627+ */
628+ g_autofree gchar * templates_dir = NULL ;
629+ const char * home_dir ;
614630
615- dir = nemo_get_templates_directory ();
616- if (!g_file_test (dir , G_FILE_TEST_EXISTS )) {
617- g_mkdir (dir , DEFAULT_NEMO_DIRECTORY_MODE );
618- }
619- g_free (dir );
631+ templates_dir = nemo_get_xdg_dir ("TEMPLATES" );
632+ home_dir = g_get_home_dir ();
633+
634+ if (g_strcmp0 (templates_dir , home_dir ) == 0 ) {
635+ // XDG_TEMPLATES_DIR is $HOME/, fix it.
636+ g_autofree gchar * fixed_templates_dir = NULL ;
637+ fixed_templates_dir = g_build_filename (home_dir , "Templates" , NULL );
638+ g_mkdir (fixed_templates_dir , DEFAULT_NEMO_DIRECTORY_MODE );
639+
640+ update_xdg_user_dir ("TEMPLATES" , fixed_templates_dir );
641+ g_reload_user_special_dirs_cache ();
642+ update_xdg_dir_cache ();
643+ } else {
644+ // XDG_TEMPLATES_DIR is reasonable, let's make sure it exists.
645+ g_mkdir (templates_dir , DEFAULT_NEMO_DIRECTORY_MODE );
646+ }
620647}
621648
622649char *
0 commit comments