@@ -65,17 +65,6 @@ def wrapper(*args):
65
65
ICONS_DIR = os .path .join (ICE_DIR , "icons" )
66
66
BROWSER_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 , BROWSER_TYPE_ZEN_FLATPAK = range (10 )
67
67
68
- class ei_task :
69
- def __init__ (self , result_callback , update_callback , builder , webAppLauncherSelf , window , task ):
70
- self .result_callback = result_callback
71
- self .update_callback = update_callback
72
- self .builder = builder
73
- self .webAppLauncherSelf = webAppLauncherSelf
74
- self .path = ""
75
- self .window = window
76
- self .task = task
77
- self .result = "error"
78
-
79
68
class Browser :
80
69
81
70
def __init__ (self , browser_type , name , exec_path , test_path ):
@@ -171,7 +160,7 @@ def get_webapps(self):
171
160
for filename in os .listdir (APPS_DIR ):
172
161
if filename .lower ().startswith ("webapp-" ) and filename .endswith (".desktop" ):
173
162
path = os .path .join (APPS_DIR , filename )
174
- codename = filename . replace ( "webapp-" , "" ). replace ( "WebApp-" , "" ). replace ( ".desktop" , "" )
163
+ codename = get_codename ( path )
175
164
if not os .path .isdir (path ):
176
165
try :
177
166
webapp = WebAppLauncher (path , codename )
@@ -319,8 +308,8 @@ def create_webapp(self, name, url, icon, category, browser, custom_parameters, i
319
308
falkon_orig_prof_dir = os .path .join (os .path .expanduser ("~/.config/falkon/profiles" ), codename )
320
309
os .symlink (falkon_profile_path , falkon_orig_prof_dir )
321
310
322
-
323
- def get_exec_string (self , browser , codename , custom_parameters , icon , isolate_profile , navbar , privatewindow , url ):
311
+ @ staticmethod
312
+ def get_exec_string ( browser , codename , custom_parameters , icon , isolate_profile , navbar , privatewindow , url ):
324
313
if browser .browser_type in [BROWSER_TYPE_FIREFOX , BROWSER_TYPE_FIREFOX_FLATPAK , BROWSER_TYPE_FIREFOX_SNAP , BROWSER_TYPE_ZEN_FLATPAK ]:
325
314
# Firefox based
326
315
if browser .browser_type == BROWSER_TYPE_FIREFOX :
@@ -564,61 +553,41 @@ def download_favicon(url):
564
553
return images
565
554
566
555
@_async
567
- def export_config ( ei_task_info : ei_task ):
568
- # The export process in the background.
556
+ def export_webapps ( callback , path ):
557
+ # The background export process
569
558
try :
570
- # Search all files
571
- files = get_all_desktop_files () + get_all_icons ()
572
- total = len (files )
573
- update_interval = 1 if int (total / 100 ) < 1 else int (total / 100 )
574
-
559
+ desktop_files = get_all_desktop_files ()
575
560
# Write the .tar.gz file
576
- with tarfile .open (ei_task_info .path , "w:gz" ) as tar :
577
- counter = 0
578
- for file in files :
579
- tar .add (file ["full_path" ], arcname = file ["arcname" ])
580
- if counter % update_interval == 0 :
581
- progress = round (counter / total , 2 )
582
- GLib .idle_add (ei_task_info .update_callback , ei_task_info , progress )
583
-
584
- counter += 1
585
-
586
- GLib .idle_add (ei_task_info .update_callback , ei_task_info , 1 )
587
- ei_task_info .result = "ok"
561
+ with tarfile .open (path , "w:gz" ) as tar :
562
+ for desktop_file in desktop_files :
563
+ tar .add (desktop_file ["full_path" ], arcname = desktop_file ["arcname" ])
564
+ tar .add (ICONS_DIR , "ice/icons/" )
565
+ result = "ok"
588
566
except Exception as e :
589
567
print (e )
590
- ei_task_info . result = "error"
591
-
592
- GLib . idle_add ( ei_task_info . result_callback , ei_task_info )
568
+ result = "error"
569
+
570
+ callback ( result , "export" , path )
593
571
594
572
@_async
595
- def import_config ( ei_task_info : ei_task ):
596
- # The import process in the background.
573
+ def import_webapps ( callback , path ):
574
+ # The background import process
597
575
try :
598
- with tarfile .open (ei_task_info .path , "r:gz" ) as tar :
576
+ result = "ok"
577
+ with tarfile .open (path , "r:gz" ) as tar :
599
578
files = tar .getnames ()
600
- total = len (files )
601
579
base_dir = os .path .dirname (ICE_DIR )
602
- update_interval = 1 if int (total / 100 ) < 1 else int (total / 100 )
603
- counter = 0
604
580
for file in files :
605
581
tar .extract (file , base_dir )
606
582
if file .startswith ("applications/" ):
607
- # Rewrite the "Exec" section. This is necessary if the username differs.
583
+ # Rewrite the "Exec" section. It will apply the new paths and will search for browsers
608
584
path = os .path .join (base_dir , file )
609
- update_exec_path (path )
610
-
611
- if counter % update_interval == 0 :
612
- progress = round (counter / total , 2 )
613
- GLib .idle_add (ei_task_info .update_callback , ei_task_info , progress )
614
- counter += 1
615
- GLib .idle_add (ei_task_info .update_callback , ei_task_info , 1 )
616
- ei_task_info .result = "ok"
585
+ update_imported_desktop (path )
617
586
except Exception as e :
618
587
print (e )
619
- ei_task_info . result = "error"
588
+ result = "error"
620
589
621
- GLib . idle_add ( ei_task_info . result_callback , ei_task_info )
590
+ callback ( result , "import" , path )
622
591
623
592
624
593
def get_all_desktop_files ():
@@ -631,45 +600,42 @@ def get_all_desktop_files():
631
600
files .append ({"full_path" :full_path , "arcname" :arcname })
632
601
return files
633
602
634
- def get_all_icons ():
635
- # List all the files in a directory.
636
- files = []
637
- for root , dirs , filenames in os .walk (ICONS_DIR ):
638
- for filename in filenames :
639
- full_path = os .path .join (root , filename )
640
- arcname = ""
641
- arcname += os .path .relpath (full_path , os .path .dirname (ICE_DIR ))
642
- files .append ({"full_path" :full_path , "arcname" :arcname })
643
- return files
644
-
645
603
646
604
def get_codename (path ):
647
- codename = os .path .basename (path )
648
- codename = codename .replace (".desktop" , "" )
649
- codename = codename .replace ("WebApp-" , "" )
650
- codename = codename .replace ("webapp-" , "" )
605
+ filename = os .path .basename (path )
606
+ codename = filename .replace (".desktop" , "" ).replace ("WebApp-" , "" ).replace ("webapp-" , "" )
651
607
return codename
652
608
653
- def update_exec_path (path ):
654
- # This updates the 'exec' section of an imported web application or creates the profile directory for it.
655
- config = configparser .RawConfigParser ()
656
- config .optionxform = str
657
- config .read (path )
658
- codename = get_codename (path )
659
- webapp = WebAppLauncher (path , codename )
660
- browsers = WebAppManager .get_supported_browsers ()
609
+ def update_imported_desktop (path ):
610
+ webapp = WebAppLauncher (path , get_codename (path ))
661
611
if "/" in webapp .icon :
662
612
# Update Icon Path
663
- iconpath = ICONS_DIR + "/" + os .path .basename (webapp .icon )
664
- config .set ("Desktop Entry" , "Icon" , iconpath )
613
+ iconpath = os .path .join (ICONS_DIR , os .path .basename (webapp .icon ))
665
614
else :
666
615
iconpath = webapp .icon
667
616
668
- browser = next ((browser for browser in browsers if browser .name == webapp .web_browser ), None )
669
- new_exec_line = WebAppManager .get_exec_string (None , browser , codename , webapp .custom_parameters , iconpath , webapp .isolate_profile , webapp .navbar , webapp .privatewindow , webapp .url )
670
- config .set ("Desktop Entry" , "Exec" , new_exec_line )
671
- with open (path , 'w' ) as configfile :
672
- config .write (configfile , space_around_delimiters = False )
617
+ # Check if the browser is installed
618
+ browsers = WebAppManager .get_supported_browsers ()
619
+ configured_browser = next ((browser for browser in browsers if browser .name == webapp .web_browser ), None )
620
+ if os .path .exists (configured_browser .test_path ) == False :
621
+ # If the browser is not installed, search another browser.
622
+ # 1. Sort browsers by same browser type
623
+ # 2. Sort the browsers by similarity of the name of the missing browser
624
+ similar_browsers = browsers
625
+ similar_browsers .sort (key = lambda browser : (
626
+ browser .browser_type == configured_browser .browser_type ,
627
+ configured_browser .name .split (" " )[0 ].lower () not in browser .name .lower ()
628
+ ))
629
+ configured_browser = None
630
+ for browser in similar_browsers :
631
+ if os .path .exists (browser .test_path ):
632
+ configured_browser = browser
633
+ break
634
+
635
+ print (webapp .web_browser , "-Browser not installed" )
636
+
637
+ WebAppManager .edit_webapp (WebAppManager , path , webapp .name , configured_browser , webapp .url , iconpath , webapp .category ,
638
+ webapp .custom_parameters , webapp .codename , webapp .isolate_profile , webapp .navbar , webapp .privatewindow )
673
639
674
640
if __name__ == "__main__" :
675
641
download_favicon (sys .argv [1 ])
0 commit comments