Commit d1aa6aa6e7f2358212d7eeb3f0710fd0e2c711a5
1 parent
acf54665
Exists in
master
and in
2 other branches
Creating session file when a desktop icon is built with default session.
Showing
4 changed files
with
74 additions
and
41 deletions
Show diff stats
src/include/v3270/keyfile.h
@@ -42,9 +42,12 @@ | @@ -42,9 +42,12 @@ | ||
42 | 42 | ||
43 | typedef struct _V3270KeyFile V3270KeyFile; | 43 | typedef struct _V3270KeyFile V3270KeyFile; |
44 | 44 | ||
45 | + gchar * v3270_keyfile_get_default_filename(void); | ||
46 | + | ||
45 | V3270KeyFile * v3270_key_file_open(GtkWidget *terminal, const gchar *name, GError **error); | 47 | V3270KeyFile * v3270_key_file_open(GtkWidget *terminal, const gchar *name, GError **error); |
46 | void v3270_key_file_close(GtkWidget *terminal); | 48 | void v3270_key_file_close(GtkWidget *terminal); |
47 | void v3270_key_file_save(GtkWidget *terminal); | 49 | void v3270_key_file_save(GtkWidget *terminal); |
50 | + void v3270_key_file_save_to_file(GtkWidget * terminal, const gchar *filename); | ||
48 | 51 | ||
49 | /// @brief Get current key filename | 52 | /// @brief Get current key filename |
50 | const gchar * v3270_key_file_get_filename(GtkWidget *terminal); | 53 | const gchar * v3270_key_file_get_filename(GtkWidget *terminal); |
src/objects/os/linux/savedesktopicon.c
@@ -39,6 +39,7 @@ | @@ -39,6 +39,7 @@ | ||
39 | #include <v3270/keyfile.h> | 39 | #include <v3270/keyfile.h> |
40 | #include <lib3270.h> | 40 | #include <lib3270.h> |
41 | #include <lib3270/log.h> | 41 | #include <lib3270/log.h> |
42 | + #include <lib3270/properties.h> | ||
42 | 43 | ||
43 | static GtkWidget * factory(V3270SimpleAction *action, GtkWidget *terminal); | 44 | static GtkWidget * factory(V3270SimpleAction *action, GtkWidget *terminal); |
44 | static void response(GtkWidget *dialog, gint response_id, GtkWidget *terminal); | 45 | static void response(GtkWidget *dialog, gint response_id, GtkWidget *terminal); |
@@ -196,6 +197,43 @@ X-Desktop-File-Install-Version=0.23 | @@ -196,6 +197,43 @@ X-Desktop-File-Install-Version=0.23 | ||
196 | return dialog; | 197 | return dialog; |
197 | } | 198 | } |
198 | 199 | ||
200 | + static gchar * get_filename(GtkWidget *terminal) { | ||
201 | + | ||
202 | + g_autofree gchar * defname = v3270_keyfile_get_default_filename(); | ||
203 | + const gchar * current = v3270_key_file_get_filename(terminal); | ||
204 | + | ||
205 | + // If is not the default name, return it. | ||
206 | + if(strcmp(defname,current)) { | ||
207 | + return g_strdup(current); | ||
208 | + } | ||
209 | + | ||
210 | + // It's the default one, create a new one on the user_config dir | ||
211 | + g_autofree gchar * config_path = g_build_filename(g_get_user_config_dir(),G_STRINGIFY(PRODUCT_NAME),NULL); | ||
212 | + g_mkdir_with_parents(config_path,0644); | ||
213 | + | ||
214 | + // Use the hostname | ||
215 | + const char * hostname = lib3270_host_get_name(v3270_get_session(terminal)); | ||
216 | + if(!hostname) { | ||
217 | + hostname = "session"; | ||
218 | + } | ||
219 | + | ||
220 | + // Build the filename | ||
221 | + gchar *filename = g_strconcat(config_path,G_DIR_SEPARATOR_S,hostname,".3270",NULL); | ||
222 | + | ||
223 | + unsigned int index = 0; | ||
224 | + while(g_file_test(filename,G_FILE_TEST_EXISTS)) { | ||
225 | + g_free(filename); | ||
226 | + filename = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s.%u.3270",config_path,hostname,++index); | ||
227 | + } | ||
228 | + | ||
229 | + v3270_key_file_save_to_file(terminal,filename); | ||
230 | + | ||
231 | + g_message("New session file create at \"%s\"",filename); | ||
232 | + | ||
233 | + return filename; | ||
234 | + | ||
235 | + } | ||
236 | + | ||
199 | void response(GtkWidget *dialog, gint response_id, GtkWidget *terminal) { | 237 | void response(GtkWidget *dialog, gint response_id, GtkWidget *terminal) { |
200 | 238 | ||
201 | debug("%s(%d)",__FUNCTION__,response_id); | 239 | debug("%s(%d)",__FUNCTION__,response_id); |
@@ -236,37 +274,7 @@ X-Desktop-File-Install-Version=0.23 | @@ -236,37 +274,7 @@ X-Desktop-File-Install-Version=0.23 | ||
236 | } | 274 | } |
237 | 275 | ||
238 | // Get session filename | 276 | // Get session filename |
239 | - /* | ||
240 | - const gchar * session_file = v3270_get_session_filename(terminal); | ||
241 | - | ||
242 | - if(!session_file) { | ||
243 | - | ||
244 | - // No session file, create one. | ||
245 | - | ||
246 | - // Check for configdir | ||
247 | - g_autofree gchar * configdir = g_build_filename(g_get_user_config_dir(),G_STRINGIFY(PRODUCT_NAME),"sessions",NULL); | ||
248 | - g_mkdir_with_parents(configdir,0755); | ||
249 | - | ||
250 | - // Create a base name | ||
251 | - g_autofree gchar * basename = g_path_get_basename(gtk_entry_get_text(GTK_ENTRY(inputs[0]))); | ||
252 | - | ||
253 | - gchar *ptr = strrchr(basename,'.'); | ||
254 | - if(ptr) | ||
255 | - *ptr = 0; | ||
256 | - | ||
257 | - ix = time(NULL); | ||
258 | - gchar * new_session_file = g_strdup_printf("%s/%s.3270",configdir,basename); | ||
259 | - while(!g_file_test(new_session_file,G_FILE_TEST_EXISTS)) { | ||
260 | - g_free(new_session_file); | ||
261 | - new_session_file = g_strdup_printf("%s/%s_%08lx.3270",configdir,basename,(unsigned long) ix++); | ||
262 | - } | ||
263 | - | ||
264 | - g_message("Saving session to %s",new_session_file); | ||
265 | - v3270_set_session_filename(terminal,new_session_file); | ||
266 | - g_free(new_session_file); | ||
267 | - | ||
268 | - } | ||
269 | - */ | 277 | + g_autofree gchar * filename = get_filename(terminal); |
270 | 278 | ||
271 | // Get program file name | 279 | // Get program file name |
272 | // https://stackoverflow.com/questions/4517425/how-to-get-program-path | 280 | // https://stackoverflow.com/questions/4517425/how-to-get-program-path |
@@ -279,7 +287,7 @@ X-Desktop-File-Install-Version=0.23 | @@ -279,7 +287,7 @@ X-Desktop-File-Install-Version=0.23 | ||
279 | if(bytes >= 0) | 287 | if(bytes >= 0) |
280 | buffer[bytes] = '\0'; | 288 | buffer[bytes] = '\0'; |
281 | 289 | ||
282 | - g_autofree gchar * exec_line = g_strdup_printf("%s \"%s\"",buffer,v3270_key_file_get_filename(terminal)); | 290 | + g_autofree gchar * exec_line = g_strdup_printf("%s \"%s\"",buffer,filename); |
283 | g_key_file_set_string(keyfile,"Desktop Entry","Exec",exec_line); | 291 | g_key_file_set_string(keyfile,"Desktop Entry","Exec",exec_line); |
284 | 292 | ||
285 | } | 293 | } |
src/objects/window/keyfile.c
@@ -189,6 +189,23 @@ void v3270_key_file_close(GtkWidget *terminal) { | @@ -189,6 +189,23 @@ void v3270_key_file_close(GtkWidget *terminal) { | ||
189 | return v3270_get_session_descriptor(terminal)->key_file; | 189 | return v3270_get_session_descriptor(terminal)->key_file; |
190 | } | 190 | } |
191 | 191 | ||
192 | + void v3270_key_file_save_to_file(GtkWidget * terminal, const gchar *filename) { | ||
193 | + | ||
194 | + V3270KeyFile * new_session = (V3270KeyFile *) g_malloc0(sizeof(struct _V3270KeyFile) + strlen(filename)); | ||
195 | + V3270KeyFile * old_session = g_object_get_data(G_OBJECT(terminal),"session-descriptor"); | ||
196 | + | ||
197 | + if(old_session) { | ||
198 | + *new_session = *old_session; | ||
199 | + } | ||
200 | + | ||
201 | + strcpy(new_session->filename,filename); | ||
202 | + new_session->key_file = g_key_file_new(); | ||
203 | + | ||
204 | + g_object_set_data_full(G_OBJECT(terminal),"session-descriptor",new_session,(GDestroyNotify) close_keyfile); | ||
205 | + v3270_key_file_save(terminal); | ||
206 | + | ||
207 | + } | ||
208 | + | ||
192 | void v3270_key_file_save(GtkWidget *terminal) { | 209 | void v3270_key_file_save(GtkWidget *terminal) { |
193 | 210 | ||
194 | V3270KeyFile *session = v3270_get_session_descriptor(terminal); | 211 | V3270KeyFile *session = v3270_get_session_descriptor(terminal); |
@@ -258,6 +275,17 @@ void v3270_key_file_close(GtkWidget *terminal) { | @@ -258,6 +275,17 @@ void v3270_key_file_close(GtkWidget *terminal) { | ||
258 | 275 | ||
259 | } | 276 | } |
260 | 277 | ||
278 | + gchar * v3270_keyfile_get_default_filename(void) { | ||
279 | + | ||
280 | + gchar * filename = g_build_filename(g_get_user_config_dir(),"default.3270",NULL); | ||
281 | + | ||
282 | + g_autofree gchar * compatible = g_build_filename(g_get_user_config_dir(),G_STRINGIFY(PRODUCT_NAME) ".conf",NULL); | ||
283 | + if(g_file_test(compatible,G_FILE_TEST_IS_REGULAR)) | ||
284 | + g_rename(compatible,filename); | ||
285 | + | ||
286 | + return filename; | ||
287 | + } | ||
288 | + | ||
261 | gchar * v3270_key_file_build_filename(GtkWidget *terminal) { | 289 | gchar * v3270_key_file_build_filename(GtkWidget *terminal) { |
262 | 290 | ||
263 | const gchar * filename = v3270_key_file_get_filename(terminal); | 291 | const gchar * filename = v3270_key_file_get_filename(terminal); |
@@ -271,7 +299,7 @@ void v3270_key_file_close(GtkWidget *terminal) { | @@ -271,7 +299,7 @@ void v3270_key_file_close(GtkWidget *terminal) { | ||
271 | const char * hostname = lib3270_host_get_name(v3270_get_session(terminal)); | 299 | const char * hostname = lib3270_host_get_name(v3270_get_session(terminal)); |
272 | debug("Hostname=\"%s\"",hostname); | 300 | debug("Hostname=\"%s\"",hostname); |
273 | 301 | ||
274 | - gchar * name = g_strconcat(folder,G_DIR_SEPARATOR_S,hostname,".3270",NULL); | 302 | + gchar * name = g_strconcat(folder,G_DIR_SEPARATOR_S,(hostname ? hostname : "session"),".3270",NULL); |
275 | unsigned int index = 0; | 303 | unsigned int index = 0; |
276 | while(g_file_test(name,G_FILE_TEST_EXISTS)) { | 304 | while(g_file_test(name,G_FILE_TEST_EXISTS)) { |
277 | g_free(name); | 305 | g_free(name); |
src/objects/window/terminal.c
@@ -135,15 +135,9 @@ | @@ -135,15 +135,9 @@ | ||
135 | } else { | 135 | } else { |
136 | 136 | ||
137 | // No session file, use the default one. | 137 | // No session file, use the default one. |
138 | - g_autofree gchar * compatible = g_build_filename(g_get_user_config_dir(),G_STRINGIFY(PRODUCT_NAME) ".conf",NULL); | ||
139 | - g_autofree gchar * filename = g_build_filename(g_get_user_config_dir(),"default.3270",NULL); | ||
140 | - | ||
141 | - if(g_file_test(compatible,G_FILE_TEST_IS_REGULAR)) | ||
142 | - { | ||
143 | - g_rename(compatible,filename); | ||
144 | - } | ||
145 | - | 138 | + gchar * filename = v3270_keyfile_get_default_filename(); |
146 | v3270_key_file_open(terminal,filename,&error); | 139 | v3270_key_file_open(terminal,filename,&error); |
140 | + g_free(filename); | ||
147 | 141 | ||
148 | } | 142 | } |
149 | 143 |