@@ -64,17 +64,6 @@ def wrapper(*args):
6464ICONS_DIR = os .path .join (ICE_DIR , "icons" )
6565BROWSER_TYPE_FIREFOX , BROWSER_TYPE_FIREFOX_FLATPAK , BROWSER_TYPE_FIREFOX_SNAP , BROWSER_TYPE_LIBREWOLF_FLATPAK , BROWSER_TYPE_WATERFOX_FLATPAK , BROWSER_TYPE_FLOORP_FLATPAK , BROWSER_TYPE_CHROMIUM , BROWSER_TYPE_EPIPHANY , BROWSER_TYPE_FALKON = range (9 )
6666
67- class ei_task :
68- def __init__ (self , result_callback , update_callback , builder , webAppLauncherSelf , window , task ):
69- self .result_callback = result_callback
70- self .update_callback = update_callback
71- self .builder = builder
72- self .webAppLauncherSelf = webAppLauncherSelf
73- self .path = ""
74- self .window = window
75- self .task = task
76- self .result = "error"
77-
7867class Browser :
7968
8069 def __init__ (self , browser_type , name , exec_path , test_path ):
@@ -170,7 +159,7 @@ def get_webapps(self):
170159 for filename in os .listdir (APPS_DIR ):
171160 if filename .lower ().startswith ("webapp-" ) and filename .endswith (".desktop" ):
172161 path = os .path .join (APPS_DIR , filename )
173- codename = filename . replace ( "webapp-" , "" ). replace ( "WebApp-" , "" ). replace ( ".desktop" , "" )
162+ codename = get_codename ( path )
174163 if not os .path .isdir (path ):
175164 try :
176165 webapp = WebAppLauncher (path , codename )
@@ -315,8 +304,8 @@ def create_webapp(self, name, url, icon, category, browser, custom_parameters, i
315304 falkon_orig_prof_dir = os .path .join (os .path .expanduser ("~/.config/falkon/profiles" ), codename )
316305 os .symlink (falkon_profile_path , falkon_orig_prof_dir )
317306
318-
319- def get_exec_string (self , browser , codename , custom_parameters , icon , isolate_profile , navbar , privatewindow , url ):
307+ @ staticmethod
308+ def get_exec_string ( browser , codename , custom_parameters , icon , isolate_profile , navbar , privatewindow , url ):
320309 if browser .browser_type in [BROWSER_TYPE_FIREFOX , BROWSER_TYPE_FIREFOX_FLATPAK , BROWSER_TYPE_FIREFOX_SNAP ]:
321310 # Firefox based
322311 if browser .browser_type == BROWSER_TYPE_FIREFOX :
@@ -558,61 +547,41 @@ def download_favicon(url):
558547 return images
559548
560549@_async
561- def export_config ( ei_task_info : ei_task ):
562- # The export process in the background.
550+ def export_webapps ( callback , path ):
551+ # The background export process
563552 try :
564- # Search all files
565- files = get_all_desktop_files () + get_all_icons ()
566- total = len (files )
567- update_interval = 1 if int (total / 100 ) < 1 else int (total / 100 )
568-
553+ desktop_files = get_all_desktop_files ()
569554 # Write the .tar.gz file
570- with tarfile .open (ei_task_info .path , "w:gz" ) as tar :
571- counter = 0
572- for file in files :
573- tar .add (file ["full_path" ], arcname = file ["arcname" ])
574- if counter % update_interval == 0 :
575- progress = round (counter / total , 2 )
576- GLib .idle_add (ei_task_info .update_callback , ei_task_info , progress )
577-
578- counter += 1
579-
580- GLib .idle_add (ei_task_info .update_callback , ei_task_info , 1 )
581- ei_task_info .result = "ok"
555+ with tarfile .open (path , "w:gz" ) as tar :
556+ for desktop_file in desktop_files :
557+ tar .add (desktop_file ["full_path" ], arcname = desktop_file ["arcname" ])
558+ tar .add (ICONS_DIR , "ice/icons/" )
559+ result = "ok"
582560 except Exception as e :
583561 print (e )
584- ei_task_info . result = "error"
585-
586- GLib . idle_add ( ei_task_info . result_callback , ei_task_info )
562+ result = "error"
563+
564+ callback ( result , "export" , path )
587565
588566@_async
589- def import_config ( ei_task_info : ei_task ):
590- # The import process in the background.
567+ def import_webapps ( callback , path ):
568+ # The background import process
591569 try :
592- with tarfile .open (ei_task_info .path , "r:gz" ) as tar :
570+ result = "ok"
571+ with tarfile .open (path , "r:gz" ) as tar :
593572 files = tar .getnames ()
594- total = len (files )
595573 base_dir = os .path .dirname (ICE_DIR )
596- update_interval = 1 if int (total / 100 ) < 1 else int (total / 100 )
597- counter = 0
598574 for file in files :
599575 tar .extract (file , base_dir )
600576 if file .startswith ("applications/" ):
601- # Rewrite the "Exec" section. This is necessary if the username differs.
577+ # Rewrite the "Exec" section. It will apply the new paths and will search for browsers
602578 path = os .path .join (base_dir , file )
603- update_exec_path (path )
604-
605- if counter % update_interval == 0 :
606- progress = round (counter / total , 2 )
607- GLib .idle_add (ei_task_info .update_callback , ei_task_info , progress )
608- counter += 1
609- GLib .idle_add (ei_task_info .update_callback , ei_task_info , 1 )
610- ei_task_info .result = "ok"
579+ update_imported_desktop (path )
611580 except Exception as e :
612581 print (e )
613- ei_task_info . result = "error"
582+ result = "error"
614583
615- GLib . idle_add ( ei_task_info . result_callback , ei_task_info )
584+ callback ( result , "import" , path )
616585
617586
618587def get_all_desktop_files ():
@@ -625,45 +594,42 @@ def get_all_desktop_files():
625594 files .append ({"full_path" :full_path , "arcname" :arcname })
626595 return files
627596
628- def get_all_icons ():
629- # List all the files in a directory.
630- files = []
631- for root , dirs , filenames in os .walk (ICONS_DIR ):
632- for filename in filenames :
633- full_path = os .path .join (root , filename )
634- arcname = ""
635- arcname += os .path .relpath (full_path , os .path .dirname (ICE_DIR ))
636- files .append ({"full_path" :full_path , "arcname" :arcname })
637- return files
638-
639597
640598def get_codename (path ):
641- codename = os .path .basename (path )
642- codename = codename .replace (".desktop" , "" )
643- codename = codename .replace ("WebApp-" , "" )
644- codename = codename .replace ("webapp-" , "" )
599+ filename = os .path .basename (path )
600+ codename = filename .replace (".desktop" , "" ).replace ("WebApp-" , "" ).replace ("webapp-" , "" )
645601 return codename
646602
647- def update_exec_path (path ):
648- # This updates the 'exec' section of an imported web application or creates the profile directory for it.
649- config = configparser .RawConfigParser ()
650- config .optionxform = str
651- config .read (path )
652- codename = get_codename (path )
653- webapp = WebAppLauncher (path , codename )
654- browsers = WebAppManager .get_supported_browsers ()
603+ def update_imported_desktop (path ):
604+ webapp = WebAppLauncher (path , get_codename (path ))
655605 if "/" in webapp .icon :
656606 # Update Icon Path
657- iconpath = ICONS_DIR + "/" + os .path .basename (webapp .icon )
658- config .set ("Desktop Entry" , "Icon" , iconpath )
607+ iconpath = os .path .join (ICONS_DIR , os .path .basename (webapp .icon ))
659608 else :
660609 iconpath = webapp .icon
661610
662- browser = next ((browser for browser in browsers if browser .name == webapp .web_browser ), None )
663- new_exec_line = WebAppManager .get_exec_string (None , browser , codename , webapp .custom_parameters , iconpath , webapp .isolate_profile , webapp .navbar , webapp .privatewindow , webapp .url )
664- config .set ("Desktop Entry" , "Exec" , new_exec_line )
665- with open (path , 'w' ) as configfile :
666- config .write (configfile , space_around_delimiters = False )
611+ # Check if the browser is installed
612+ browsers = WebAppManager .get_supported_browsers ()
613+ configured_browser = next ((browser for browser in browsers if browser .name == webapp .web_browser ), None )
614+ if os .path .exists (configured_browser .test_path ) == False :
615+ # If the browser is not installed, search another browser.
616+ # 1. Sort browsers by same browser type
617+ # 2. Sort the browsers by similarity of the name of the missing browser
618+ similar_browsers = browsers
619+ similar_browsers .sort (key = lambda browser : (
620+ browser .browser_type == configured_browser .browser_type ,
621+ configured_browser .name .split (" " )[0 ].lower () not in browser .name .lower ()
622+ ))
623+ configured_browser = None
624+ for browser in similar_browsers :
625+ if os .path .exists (browser .test_path ):
626+ configured_browser = browser
627+ break
628+
629+ print (webapp .web_browser , "-Browser not installed" )
630+
631+ WebAppManager .edit_webapp (WebAppManager , path , webapp .name , configured_browser , webapp .url , iconpath , webapp .category ,
632+ webapp .custom_parameters , webapp .codename , webapp .isolate_profile , webapp .navbar , webapp .privatewindow )
667633
668634if __name__ == "__main__" :
669635 download_favicon (sys .argv [1 ])
0 commit comments