2020public class AppEditor.AppInfoViewSaver : Object {
2121 public AppInfoView target { get ; set ; }
2222
23- public static DesktopApp ? create_new_local_app (AppCategory ? category ) {
23+ // TODO: make these functions fully async
24+ public static DesktopApp ? create_new_local_app (AppCategory ? category ) throws Error {
2425 string lang = Intl . get_language_names ()[0 ];
2526
2627 var key = new KeyFile ();
@@ -32,19 +33,43 @@ public class AppEditor.AppInfoViewSaver : Object {
3233 key. set_string_list (KeyFileDesktop . GROUP , KeyFileDesktop . KEY_CATEGORIES , categories);
3334 }
3435
35- string path = AppDirectoryScanner . get_config_path ();
36-
37- uint next_index = get_next_local_app_index (path);
38- string new_filename = Path . build_filename (path, " %s%u%s " . printf (DesktopApp . LOCAL_APP_NAME_PREFIX , next_index, DesktopApp . LOCAL_APP_NAME_SUFFIX ));
36+ string config_path = AppDirectoryScanner . get_config_path ();
37+ string new_filename = get_next_local_filename (config_path);
3938 try {
4039 key. save_to_file (new_filename);
4140
4241 var app_info = new DesktopAppInfo .from_filename (new_filename);
4342 var desktop_app = new DesktopApp (app_info);
4443 return desktop_app;
4544 } catch (Error e) {
46- return null ;
45+ throw e;
46+ }
47+ }
48+
49+ public static DesktopApp ? create_new_clone_app (DesktopApp app ) throws Error {
50+ string target_filename = app. info. get_filename ();
51+ string contents;
52+ try {
53+ FileUtils . get_contents (target_filename, out contents);
54+ } catch (FileError e) {
55+ throw e;
56+ }
57+
58+ string config_path = AppDirectoryScanner . get_config_path ();
59+ string new_filename = get_next_local_filename (config_path);
60+ var file = File . new_for_path (new_filename);
61+ try {
62+ var os = file. create (FileCreateFlags . NONE );
63+ os. write_all (contents. data, null );
64+ os. close ();
65+
66+ var app_info = new DesktopAppInfo .from_filename (new_filename);
67+ var desktop_app = new DesktopApp (app_info);
68+ return desktop_app;
69+ } catch (Error e) {
70+ throw e;
4771 }
72+
4873 }
4974
5075 public async void save () throws Error {
@@ -125,6 +150,11 @@ public class AppEditor.AppInfoViewSaver : Object {
125150 return str. escape ();
126151 }
127152
153+ private static string get_next_local_filename (string path ) {
154+ uint next_index = get_next_local_app_index (path);
155+ return Path . build_filename (path, " %s%u%s " . printf (DesktopApp . LOCAL_APP_NAME_PREFIX , next_index, DesktopApp . LOCAL_APP_NAME_SUFFIX ));
156+ }
157+
128158 private static uint get_next_local_app_index (string path ) {
129159 uint index = 1 ;
130160
0 commit comments