44from gi .repository import GLib , Gio
55from localization import _ , CACHE , DATA , home , system_dir , flatpak , snap
66import argparse
7+ import shutil
78
89parser = argparse .ArgumentParser ()
910parser .add_argument ("-s" , "--save" , help = "Save the current configuration" , action = "store_true" )
3637settings = Gio .Settings .new_with_path ("io.github.vikdevelop.SaveDesktop" , "/io/github/vikdevelop/SaveDesktop/" )
3738cache_replacing = f'{ CACHE } '
3839config = cache_replacing .replace ("cache/tmp" , "config/glib-2.0/settings" )
40+ flatpak_app_data = settings ["disabled-flatpak-apps-data" ]
3941
4042class Save :
4143 def __init__ (self ):
44+ print ("saving settings from the Dconf database" )
4245 os .system ("dconf dump / > ./dconf-settings.ini" )
43- #os.system('cp -R {home}/.config/dconf/user ./' )
46+ print ( "saving Gtk settings" )
4447 os .system (f'cp -R { home } /.config/gtk-4.0 ./' )
4548 os .system (f'cp -R { home } /.config/gtk-3.0 ./' )
4649 if settings ["save-backgrounds" ] == True :
50+ print ("saving backgrounds" )
4751 os .system (f'cp -R { home } /.local/share/backgrounds ./' )
4852 if settings ["save-icons" ] == True :
53+ print ("saving icons" )
4954 os .system (f'cp -R { home } /.local/share/icons ./' )
5055 os .system (f'cp -R { home } /.icons ./' )
5156 if settings ["save-themes" ] == True :
57+ print ("saving themes" )
5258 os .system (f'cp -R { home } /.themes ./' )
5359 if settings ["save-fonts" ] == True :
60+ print ("saving fonts" )
5461 os .system (f'cp -R { home } /.fonts ./' )
5562 if settings ["save-desktop-folder" ] == True :
63+ print ("saving desktop folder" )
5664 if " " in GLib .get_user_special_dir (GLib .UserDirectory .DIRECTORY_DESKTOP ):
5765 desktop_with_spaces = GLib .get_user_special_dir (GLib .UserDirectory .DIRECTORY_DESKTOP )
5866 desktop_without_spaces = desktop_with_spaces .replace (" " , "*" )
5967 else :
6068 desktop_without_spaces = GLib .get_user_special_dir (GLib .UserDirectory .DIRECTORY_DESKTOP )
6169 os .system (f'cp -R { desktop_without_spaces } ./Desktop/ ' )
6270 if settings ["save-installed-flatpaks" ] == True :
71+ print ("saving list of installed Flatpak apps" )
6372 os .system ('sh /app/backup_flatpaks.sh' )
6473 if settings ["save-flatpak-data" ] == True :
74+ print ("saving user data of installed Flatpak apps" )
75+ blst = settings ["disabled-flatpak-apps-data" ]
76+ # convert GSettings property to list
77+ blist = blst
78+ # add io.github.vikdevelop.SaveDesktop to blacklist, because during saving configuration the cache folder is too large
79+ blist += ["io.github.vikdevelop.SaveDesktop" ]
80+ blacklist = blist
81+
82+ # set destination dir
6583 if os .path .exists (f"{ CACHE } /save_config" ):
66- os .system (f'cd { home } /.var/app && mkdir { CACHE } /save_config/app && cp -r `ls -A | grep -v "io.github.vikdevelop.SaveDesktop"` { CACHE } /save_config/app/' )
84+ os .makedirs (f"{ CACHE } /save_config/app" , exist_ok = True )
85+ destdir = f"{ CACHE } /save_config/app"
6786 elif os .path .exists (f"{ CACHE } /periodic_saving" ):
68- os .system (f'cd { home } /.var/app && mkdir { CACHE } /periodic_saving/app && cp -r `ls -A | grep -v "io.github.vikdevelop.SaveDesktop"` { CACHE } /periodic_saving/app/' )
87+ os .makedirs (f"{ CACHE } /periodic_saving/app" , exist_ok = True )
88+ destdir = f"{ CACHE } /periodic_saving/app"
89+
90+ # copy Flatpak apps data
91+ for item in os .listdir (f"{ home } /.var/app" ):
92+ if item not in blacklist :
93+ source_path = os .path .join (f"{ home } /.var/app" , item )
94+ destination_path = os .path .join (destdir , item )
95+ if os .path .isdir (source_path ):
96+ try :
97+ shutil .copytree (source_path , destination_path )
98+ except Exception as e :
99+ print (f"Error copying directory { source_path } : { e } " )
100+ else :
101+ try :
102+ shutil .copy2 (source_path , destination_path )
103+ except Exception as e :
104+ print (f"Error copying file { source_path } : { e } " )
105+
106+ # save user data except for the cache of the SaveDesktop app if the app is not in the "disabled-flatpak-apps-data" key of the GSettings database
107+ if not "io.github.vikdevelop.SaveDesktop" in flatpak_app_data :
108+ os .makedirs (f"{ destdir } /io.github.vikdevelop.SaveDesktop" , exist_ok = True )
109+ os .chdir (f"{ home } /.var/app/io.github.vikdevelop.SaveDesktop" )
110+ os .system (f"cp -R ./config { destdir } /io.github.vikdevelop.SaveDesktop/" )
111+ os .system (f"cp -R ./data { destdir } /io.github.vikdevelop.SaveDesktop/" )
112+ os .chdir (f"{ CACHE } /save_config" )
69113
114+ print ("saving desktop environment configuration files" )
70115 # Save configs on individual desktop environments
71116 if environment == 'GNOME' :
72117 os .system (f"cp -R { home } /.local/share/gnome-background-properties ./" )
@@ -115,6 +160,7 @@ def __init__(self):
115160 os .system (f"cp -R { home } /.local/share/wallpapers ./xdg-data/" )
116161 if settings ["save-extensions" ] == True :
117162 os .system (f"cp -R { home } /.local/share/plasma ./xdg-data/" )
163+ print ("creating configuration archive" )
118164 os .system (f"tar --exclude='cfg.sd.tar.gz' --gzip -cf cfg.sd.tar.gz ./" )
119165 if os .path .exists (f"{ CACHE } /.filedialog.json" ):
120166 with open (f"{ CACHE } /.filedialog.json" ) as j :
@@ -125,7 +171,11 @@ def __init__(self):
125171 if not settings ["periodic-import" ] == "Never2" :
126172 file = os .path .basename (j ["recent_file" ])
127173 os .system (f"cp -R ./cfg.sd.tar.gz { DATA } /synchronization/{ file } " )
174+ print ("moving the configuration archive to the user-defined directory" )
128175 os .system (f"mv ./cfg.sd.tar.gz { j ['recent_file' ]} " )
176+ if os .path .exists (f"{ CACHE } /save_config" ):
177+ os .system ("echo > done_gui" )
178+ print ("THE CONFIGURATION HAS BEEN SAVED SUCCESSFULLY!" )
129179
130180class Import :
131181 def __init__ (self ):
@@ -135,6 +185,7 @@ def __init__(self):
135185 os .system ("tar -xf %s ./" % j ["import_file" ])
136186 if not os .path .exists ("{}/.config" .format (home )):
137187 os .system (f"mkdir { home } /.config/" )
188+ print ("importing settings from the Dconf database" )
138189 if os .path .exists ("user" ):
139190 os .system (f"cp -R ./user { home } /.config/dconf/" )
140191 else :
@@ -143,22 +194,30 @@ def __init__(self):
143194 else :
144195 os .system ("echo user-db:user > temporary-profile" )
145196 os .system ('DCONF_PROFILE="$(pwd)/temporary-profile" dconf load / < dconf-settings.ini' )
197+ print ("importing list of installed Flatpak apps (them will be installed after the next login)" )
146198 os .system (f'cp ./installed_flatpaks.sh { DATA } /' )
147199 os .system (f'cp ./installed_user_flatpaks.sh { DATA } /' )
200+ print ("importing icons" )
148201 os .system (f'cp -R ./icons { home } /.local/share/' )
149- os .system (f'cp -R ./.themes { home } /' )
150202 os .system (f'cp -R ./.icons { home } /' )
203+ print ("importing themes" )
204+ os .system (f'cp -R ./.themes { home } /' )
205+ print ("importing backgrounds" )
151206 os .system (f'cp -R ./backgrounds { home } /.local/share/' )
207+ print ("importing fonts" )
152208 os .system (f'cp -R ./.fonts { home } /' )
209+ print ("importing Gtk settings" )
153210 os .system (f'cp -R ./gtk-4.0 { home } /.config/' )
154211 os .system (f'cp -R ./gtk-3.0 { home } /.config/' )
212+ print ("importing desktop directory" )
155213 os .system (f'cp -R ./Desktop/* { GLib .get_user_special_dir (GLib .UserDirectory .DIRECTORY_DESKTOP )} /' )
156214 if os .path .exists (f'{ CACHE } /import_config/app' ):
157215 with open (f"copying_flatpak_data" , "w" ) as c :
158216 c .write ("copying flatpak data ..." )
159217 elif os .path .exists (f'{ CACHE } /syncing/app' ):
160218 with open (f"copying_flatpak_data" , "w" ) as c :
161219 c .write ("copying flatpak data ..." )
220+ print ("importing desktop environment configuration files" )
162221 # Apply configs for individual desktop environments
163222 if environment == 'GNOME' :
164223 os .system (f'cp -R ./gnome-background-properties { home } /.local/share/' )
@@ -199,9 +258,10 @@ def __init__(self):
199258 os .chdir ("%s/import_config" % CACHE )
200259 os .chdir ('xdg-data' )
201260 os .system (f'cp -R ./ { home } /.local/share/' )
202- if not snap :
261+ if flatpak :
203262 self .create_flatpak_desktop ()
204263 os .system (f"echo > { CACHE } /import_config/done" )
264+ print ("THE CONFIGURATION HAS BEEN IMPORTED SUCCESSFULLY!" )
205265
206266 # Create desktop file for install Flatpaks from list
207267 def create_flatpak_desktop (self ):
0 commit comments