Commit 20eecdbe123c68d4fe1ebd65cc66764095417fed

Authored by Perry Werneck
1 parent 627823e2

Implementing linux "create desktop icon" action.

Showing 1 changed file with 120 additions and 7 deletions   Show diff stats
src/objects/linux/savedesktopicon.c
... ... @@ -56,11 +56,11 @@ StartupNotify=true
56 56 Categories=GTK;GNOME;TerminalEmulator
57 57 OnlyShowIn=GNOME;Unity
58 58 X-Desktop-File-Install-Version=0.23
59   -
60 59 */
61 60  
62 61 static const struct _entry {
63 62  
  63 + const gchar * key;
64 64 const gchar * label;
65 65 const gchar * tooltip;
66 66 gint width;
... ... @@ -75,12 +75,21 @@ X-Desktop-File-Install-Version=0.23
75 75 },
76 76  
77 77 {
  78 + .key = "Name",
78 79 .label = N_("Launcher name"),
79 80 .width = 20,
80 81 // .n_chars = 128
81 82 },
82 83  
83 84 {
  85 + .key = "GenericName",
  86 + .label = N_("Generic name"),
  87 + .width = 20,
  88 +// .n_chars = 128
  89 + },
  90 +
  91 + {
  92 + .key = "Comment",
84 93 .label = N_("Comment"),
85 94 .width = 30,
86 95 // .n_chars = 128
... ... @@ -130,6 +139,7 @@ X-Desktop-File-Install-Version=0.23
130 139 // Create entry fields
131 140 GtkWidget ** inputs = g_new0(GtkWidget *,G_N_ELEMENTS(entries));
132 141 g_object_set_data_full(G_OBJECT(dialog),"inputs",inputs,g_free);
  142 + debug("Dialog=%p inputs=%p",dialog,inputs);
133 143  
134 144 GtkGrid * grid = GTK_GRID(gtk_grid_new());
135 145  
... ... @@ -150,6 +160,8 @@ X-Desktop-File-Install-Version=0.23
150 160 gtk_grid_attach(grid,label,0,ix,1,1);
151 161  
152 162 inputs[ix] = gtk_entry_new();
  163 + debug("inputs[%u]=%p",(unsigned int) ix, inputs[ix]);
  164 +
153 165 gtk_entry_set_width_chars(GTK_ENTRY(inputs[ix]),entries[ix].width);
154 166 // gtk_entry_set_max_width_chars(GTK_ENTRY(inputs[ix]),entries[ix].n_chars);
155 167 gtk_widget_set_hexpand(inputs[ix],FALSE);
... ... @@ -166,9 +178,12 @@ X-Desktop-File-Install-Version=0.23
166 178 gtk_entry_set_placeholder_text(GTK_ENTRY(inputs[1]),G_STRINGIFY(PRODUCT_NAME));
167 179 gtk_entry_set_text(GTK_ENTRY(inputs[1]),G_STRINGIFY(PRODUCT_NAME));
168 180  
169   - gtk_entry_set_placeholder_text(GTK_ENTRY(inputs[2]),v3270_get_url(terminal));
170   - gtk_entry_set_text(GTK_ENTRY(inputs[2]),v3270_get_url(terminal));
171   - gtk_entry_set_input_hints(GTK_ENTRY(inputs[2]),GTK_INPUT_HINT_SPELLCHECK);
  181 + gtk_entry_set_placeholder_text(GTK_ENTRY(inputs[2]),G_STRINGIFY(PRODUCT_NAME));
  182 + gtk_entry_set_text(GTK_ENTRY(inputs[2]),G_STRINGIFY(PRODUCT_NAME));
  183 +
  184 + gtk_entry_set_placeholder_text(GTK_ENTRY(inputs[3]),v3270_get_url(terminal));
  185 + gtk_entry_set_text(GTK_ENTRY(inputs[3]),v3270_get_url(terminal));
  186 + gtk_entry_set_input_hints(GTK_ENTRY(inputs[3]),GTK_INPUT_HINT_SPELLCHECK);
172 187  
173 188 gtk_widget_show_all(GTK_WIDGET(grid));
174 189 return dialog;
... ... @@ -178,11 +193,109 @@ X-Desktop-File-Install-Version=0.23
178 193  
179 194 debug("%s(%d)",__FUNCTION__,response_id);
180 195  
  196 + if(response_id == GTK_RESPONSE_APPLY) {
  197 +
  198 + static const char * key_file_data =
  199 + "[Desktop Entry]\n" \
  200 + "Icon=" G_STRINGIFY(PRODUCT_NAME) "\n" \
  201 + "Terminal=false\n" \
  202 + "Type=Application\n" \
  203 + "StartupNotify=true\n" \
  204 + "Categories=GTK;GNOME;TerminalEmulator\n" \
  205 + "OnlyShowIn=GNOME;Unity\n";
  206 +
  207 + GError * error = NULL;
  208 + size_t ix;
  209 +
  210 + GKeyFile * keyfile = g_key_file_new();
  211 + g_key_file_load_from_data(keyfile,key_file_data,-1,G_KEY_FILE_NONE,NULL);
  212 +
  213 +#ifdef DEBUG
  214 + {
  215 + g_autofree gchar * dbg_data = g_key_file_to_data(keyfile,NULL,NULL);
  216 + debug("\n%s\n",dbg_data);
  217 + }
  218 +#endif // DEBUG
  219 +
  220 +
  221 + GtkWidget ** inputs = g_object_get_data(G_OBJECT(dialog),"inputs");
  222 + debug("dialog=%p inputs=%p",dialog,inputs);
  223 +
  224 + for(ix = 0; ix < G_N_ELEMENTS(entries); ix++) {
  225 + if(entries[ix].key) {
  226 + debug("inputs[%u]=%p",(unsigned int) ix, inputs[ix]);
  227 + g_key_file_set_string(keyfile,"Desktop Entry",entries[ix].key,gtk_entry_get_text(GTK_ENTRY(inputs[ix])));
  228 + }
  229 + }
  230 +
  231 + // Get session filename
  232 + const gchar * session_file = v3270_get_session_filename(terminal);
  233 +
  234 + /*
  235 + if(!session_file) {
  236 +
  237 + // No session file, create one.
  238 +
  239 + // Check for configdir
  240 + g_autofree gchar * configdir = g_build_filename(g_get_user_config_dir(),G_STRINGIFY(PRODUCT_NAME),"sessions",NULL);
  241 + g_mkdir_with_parents(configdir,0755);
181 242  
182   - gtk_widget_destroy(dialog);
  243 + // Create a base name
  244 + g_autofree gchar * basename = g_path_get_basename(gtk_entry_get_text(GTK_ENTRY(inputs[0])));
183 245  
184   - if(response_id == GTK_RESPONSE_OK) {
  246 + gchar *ptr = strrchr(basename,'.');
  247 + if(ptr)
  248 + *ptr = 0;
185 249  
  250 + ix = time(NULL);
  251 + gchar * new_session_file = g_strdup_printf("%s/%s.3270",configdir,basename);
  252 + while(!g_file_test(new_session_file,G_FILE_TEST_EXISTS)) {
  253 + g_free(new_session_file);
  254 + new_session_file = g_strdup_printf("%s/%s_%08lx.3270",configdir,basename,(unsigned long) ix++);
  255 + }
  256 +
  257 + g_message("Saving session to %s",new_session_file);
  258 + v3270_set_session_filename(terminal,new_session_file);
  259 + g_free(new_session_file);
  260 +
  261 + }
  262 + */
  263 +
  264 + // Get program file name
  265 + // https://stackoverflow.com/questions/4517425/how-to-get-program-path
  266 + {
  267 + char buffer[4096];
  268 + g_autofree gchar * pidfile = g_strdup_printf("/proc/%d/exe", getpid());
  269 +
  270 + int bytes = readlink(pidfile,buffer,4095);
  271 +
  272 + if(bytes >= 0)
  273 + buffer[bytes] = '\0';
  274 +
  275 + g_autofree gchar * exec_line = g_strdup_printf("%s \"%s\"",buffer,v3270_get_session_filename(terminal));
  276 + g_key_file_set_string(keyfile,"Desktop Entry","Exec",exec_line);
  277 +
  278 + }
  279 +
  280 + g_key_file_save_to_file(keyfile,gtk_entry_get_text(GTK_ENTRY(inputs[0])),&error);
  281 +
  282 + if(error) {
  283 +
  284 + g_message("%s",error->message);
  285 +
  286 +
  287 + g_error_free(error);
  288 +
  289 + } else {
  290 +
  291 + g_message("File \"%s\" was saved",gtk_entry_get_text(GTK_ENTRY(inputs[0])));
  292 +
  293 + }
  294 +
  295 +
  296 + g_key_file_free(keyfile);
186 297 }
187 298  
188   - }
  299 + gtk_widget_destroy(dialog);
  300 +
  301 +}
... ...