static void conf_ensure_migrated (const gchar *name) { gboolean needed = TRUE; GKeyFile *kf; gchar **list; gsize i, n; kf = g_key_file_new (); g_key_file_load_from_data_dirs (kf, "gsettings-data-convert", NULL, G_KEY_FILE_NONE, NULL); list = g_key_file_get_string_list (kf, "State", "converted", &n, NULL); if (list) { for (i = 0; i < n; i++) { if (strcmp (list[i], name) == 0) { needed = FALSE; break; } } g_strfreev (list); } g_key_file_free (kf); if (needed) g_spawn_command_line_sync ("gsettings-data-convert", NULL, NULL, NULL, NULL); }
/* * If file_name is not a full path, this function searches default paths * for the desktop file. */ VFSAppDesktop* vfs_app_desktop_new( const char* file_name ) { GKeyFile* file; gboolean load; char* relative_path; VFSAppDesktop* app = g_slice_new0( VFSAppDesktop ); app->n_ref = 1; file = g_key_file_new(); if( !file_name ) { g_key_file_free( file ); return app; } if( g_path_is_absolute( file_name ) ) { app->file_name = g_path_get_basename( file_name ); load = g_key_file_load_from_file( file, file_name, G_KEY_FILE_NONE, NULL ); } else { app->file_name = g_strdup( file_name ); relative_path = g_build_filename( "applications", app->file_name, NULL ); load = g_key_file_load_from_data_dirs( file, relative_path, NULL, G_KEY_FILE_NONE, NULL ); g_free( relative_path ); } if( load ) { app->disp_name = g_key_file_get_locale_string ( file, desktop_entry_name, "Name", NULL, NULL); app->comment = g_key_file_get_locale_string ( file, desktop_entry_name, "Comment", NULL, NULL); app->exec = g_key_file_get_string ( file, desktop_entry_name, "Exec", NULL); app->icon_name = g_key_file_get_string ( file, desktop_entry_name, "Icon", NULL); app->terminal = g_key_file_get_boolean( file, desktop_entry_name, "Terminal", NULL ); app->hidden = g_key_file_get_boolean( file, desktop_entry_name, "NoDisplay", NULL ); app->path = g_key_file_get_string ( file, desktop_entry_name, "Path", NULL); } g_key_file_free( file ); return app; }
gboolean fm_launch_desktop_entry(GAppLaunchContext* ctx, const char* file_or_id, GList* uris, GError** err) { GKeyFile* kf = g_key_file_new(); gboolean loaded; gboolean ret = FALSE; if(g_path_is_absolute(file_or_id)) loaded = g_key_file_load_from_file(kf, file_or_id, 0, err); else { char* tmp = g_strconcat("applications/", file_or_id, NULL); loaded = g_key_file_load_from_data_dirs(kf, tmp, NULL, 0, err); g_free(tmp); } if(loaded) { GList* _uris = NULL; GAppInfo* app = NULL; char* type = g_key_file_get_string(kf, G_KEY_FILE_DESKTOP_GROUP, "Type", NULL); if(type) { if(strcmp(type, "Application") == 0) app = g_desktop_app_info_new_from_keyfile(kf); else if(strcmp(type, "Link") == 0) { char* url = g_key_file_get_string(kf, G_KEY_FILE_DESKTOP_GROUP, "URL", NULL); if(url) { char* scheme = g_uri_parse_scheme(url); if(scheme) { /* Damn! this actually relies on gconf to work. */ /* FIXME: use our own way to get a usable browser later. */ app = g_app_info_get_default_for_uri_scheme(scheme); uris = _uris = g_list_prepend(NULL, url); g_free(scheme); } } } else if(strcmp(type, "Directory") == 0) { /* FIXME: how should this work? It's not defined in the spec. */ } if(app) ret = g_app_info_launch_uris(app, uris, ctx, err); } } g_key_file_free(kf); return ret; }
gboolean fm_app_info_launch (GAppInfo *appinfo, GList *files, GAppLaunchContext *launch_context, GError **error) { gboolean supported = FALSE, ret = FALSE; if (G_IS_DESKTOP_APP_INFO (appinfo)) { const char*id = g_app_info_get_id (appinfo); if (id) // this is an installed application { // load the desktop entry file to obtain more info GKeyFile *kf = g_key_file_new (); char *rel_path = g_strconcat ("applications/", id, NULL); char *full_desktop_path; supported = g_key_file_load_from_data_dirs (kf, rel_path, &full_desktop_path, 0, NULL); g_free (rel_path); if (supported) { ret = do_launch (appinfo, full_desktop_path, kf, files, launch_context, error); g_free (full_desktop_path); } g_key_file_free (kf); } else { const char *file = g_desktop_app_info_get_filename (G_DESKTOP_APP_INFO (appinfo)); if (file) // this is a desktop entry file { // load the desktop entry file to obtain more info GKeyFile *kf = g_key_file_new (); supported = g_key_file_load_from_file (kf, file, 0, NULL); if (supported) ret = do_launch (appinfo, file, kf, files, launch_context, error); g_key_file_free (kf); } else { // If this is created with fm_app_info_create_from_commandline () if (g_object_get_data (G_OBJECT (appinfo), "flags")) { supported = TRUE; ret = do_launch (appinfo, NULL, NULL, files, launch_context, error); } } } } else supported = FALSE; if (!supported) // fallback to GAppInfo::launch return g_app_info_launch (appinfo, files, launch_context, error); return ret; }
/** * egg_desktop_file_new_from_data_dirs: * @desktop_file_path: relative path to a Freedesktop-style Desktop file * @error: error pointer * * Looks for @desktop_file_path in the paths returned from * g_get_user_data_dir() and g_get_system_data_dirs(), and creates * a new #EggDesktopFile from it. * * Return value: the new #EggDesktopFile, or %NULL on error. **/ EggDesktopFile* egg_desktop_file_new_from_data_dirs(const char* desktop_file_path, GError** error) { EggDesktopFile* desktop_file; GKeyFile* key_file; char* full_path; key_file = g_key_file_new(); if (!g_key_file_load_from_data_dirs(key_file, desktop_file_path, &full_path, 0, error)) { g_key_file_free(key_file); return NULL; } desktop_file = egg_desktop_file_new_from_key_file(key_file, full_path, error); g_free(full_path); return desktop_file; }
ShellAppInfo * shell_app_system_load_from_desktop_file (ShellAppSystem *system, const char *filename, GError **error) { ShellAppInfo *appinfo; GKeyFile *keyfile; char *full_path = NULL; gboolean success; keyfile = g_key_file_new (); if (strchr (filename, '/') != NULL) { success = g_key_file_load_from_file (keyfile, filename, G_KEY_FILE_NONE, error); full_path = g_strdup (filename); } else { char *app_path = g_build_filename ("applications", filename, NULL); success = g_key_file_load_from_data_dirs (keyfile, app_path, &full_path, G_KEY_FILE_NONE, error); g_free (app_path); } if (!success) { g_key_file_free (keyfile); g_free (full_path); return NULL; } appinfo = shell_app_info_new_from_keyfile_take_ownership (keyfile, full_path); g_free (full_path); return appinfo; }
void test_loadfromfile() { GKeyFile *keyfile = g_key_file_new(),*keyfile1=g_key_file_new(); gchar *full_path = NULL; int ret; if(create_file("c:\\tempfile.txt")) { g_assert(g_key_file_load_from_file(keyfile,"c:\\tempfile.txt",0,NULL)); check_string_value (keyfile, "group1", "key1", "123"); check_string_value (keyfile, "group1", "key2", "456"); } else g_assert(FALSE); g_key_file_free(keyfile); if(create_file("tempfile.txt")) g_assert(g_key_file_load_from_data_dirs(keyfile1,"tempfile.txt",&full_path,0,NULL)); }
GKeyFile * fs_utils_get_default_element_properties (GstElement *element) { gboolean file_loaded; GKeyFile *keyfile = g_key_file_new (); gchar *filename; const gchar *factory_name = factory_name_from_element (element); filename = g_build_filename (PACKAGE, FS_MAJORMINOR, factory_name, "default-element-properties", NULL); file_loaded = g_key_file_load_from_data_dirs (keyfile, filename, NULL, G_KEY_FILE_NONE, NULL); g_free (filename); if (file_loaded) { return keyfile; } else { g_key_file_free (keyfile); return NULL; } }
static void panel_menu_items_append_from_desktop (GtkWidget *menu, char *path, char *force_name, gboolean use_icon) { GKeyFile *key_file; gboolean loaded; GtkWidget *item; char *path_freeme; char *full_path; char *uri; char *type; gboolean is_application; char *tryexec; char *icon; char *name; char *comment; path_freeme = NULL; key_file = g_key_file_new (); if (g_path_is_absolute (path)) { loaded = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, NULL); full_path = path; } else { char *lookup_file; char *desktop_path; if (!g_str_has_suffix (path, ".desktop")) { desktop_path = g_strconcat (path, ".desktop", NULL); } else { desktop_path = path; } lookup_file = g_strconcat ("applications", G_DIR_SEPARATOR_S, desktop_path, NULL); loaded = g_key_file_load_from_data_dirs (key_file, lookup_file, &path_freeme, G_KEY_FILE_NONE, NULL); full_path = path_freeme; g_free (lookup_file); if (desktop_path != path) g_free (desktop_path); } if (!loaded) { g_key_file_free (key_file); if (path_freeme) g_free (path_freeme); return; } /* For Application desktop files, respect TryExec */ type = panel_key_file_get_string (key_file, "Type"); if (!type) { g_key_file_free (key_file); if (path_freeme) g_free (path_freeme); return; } is_application = (strcmp (type, "Application") == 0); g_free (type); if (is_application) { tryexec = panel_key_file_get_string (key_file, "TryExec"); if (tryexec) { char *prog; prog = g_find_program_in_path (tryexec); g_free (tryexec); if (!prog) { /* FIXME: we could add some file monitor magic, * so that the menu items appears when the * program appears, but that's really complex * for not a huge benefit */ g_key_file_free (key_file); if (path_freeme) g_free (path_freeme); return; } g_free (prog); } } /* Now, simply build the menu item */ icon = panel_key_file_get_locale_string (key_file, "Icon"); comment = panel_key_file_get_locale_string (key_file, "Comment"); if (PANEL_GLIB_STR_EMPTY (force_name)) name = panel_key_file_get_locale_string (key_file, "Name"); else name = g_strdup (force_name); if (use_icon) { item = panel_image_menu_item_new (); } else { item = gtk_image_menu_item_new (); } setup_menu_item_with_icon (item, panel_menu_icon_get_size (), icon, NULL, NULL, name); panel_util_set_tooltip_text (item, comment); gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); g_signal_connect_data (item, "activate", G_CALLBACK (panel_menu_item_activate_desktop_file), g_strdup (full_path), (GClosureNotify) g_free, 0); g_signal_connect (G_OBJECT (item), "button_press_event", G_CALLBACK (menu_dummy_button_press_event), NULL); uri = g_filename_to_uri (full_path, NULL, NULL); setup_uri_drag (item, uri, icon, GDK_ACTION_COPY); g_free (uri); g_key_file_free (key_file); if (icon) g_free (icon); if (name) g_free (name); if (comment) g_free (comment); if (path_freeme) g_free (path_freeme); }
static void panel_menu_items_append_from_desktop (GtkWidget *menu, char *path, char *force_name) { GKeyFile *key_file; gboolean loaded; GtkWidget *item; char *path_freeme; char *full_path; char *uri; char *icon; char *name; char *comment; path_freeme = NULL; key_file = g_key_file_new (); if (g_path_is_absolute (path)) { loaded = g_key_file_load_from_file (key_file, path, G_KEY_FILE_NONE, NULL); full_path = path; } else { char *lookup_file; char *desktop_path; if (!g_str_has_suffix (path, ".desktop")) { desktop_path = g_strconcat (path, ".desktop", NULL); } else { desktop_path = path; } lookup_file = g_strconcat ("applications", G_DIR_SEPARATOR_S, desktop_path, NULL); loaded = g_key_file_load_from_data_dirs (key_file, lookup_file, &path_freeme, G_KEY_FILE_NONE, NULL); full_path = path_freeme; g_free (lookup_file); if (desktop_path != path) g_free (desktop_path); } if (!loaded) { g_key_file_free (key_file); if (path_freeme) g_free (path_freeme); return; } icon = panel_util_key_file_get_locale_string (key_file, "Icon"); comment = panel_util_key_file_get_locale_string (key_file, "Comment"); if (string_empty (force_name)) name = panel_util_key_file_get_locale_string (key_file, "Name"); else name = g_strdup (force_name); item = gtk_image_menu_item_new (); setup_menu_item_with_icon (item, panel_menu_icon_get_size (), icon, NULL, NULL, name); panel_util_set_tooltip_text (item, comment); gtk_menu_shell_append (GTK_MENU_SHELL (menu), item); g_signal_connect_data (item, "activate", G_CALLBACK (panel_menu_item_activate_desktop_file), g_strdup (full_path), (GClosureNotify) g_free, 0); g_signal_connect (G_OBJECT (item), "button_press_event", G_CALLBACK (menu_dummy_button_press_event), NULL); uri = g_filename_to_uri (full_path, NULL, NULL); setup_uri_drag (item, uri, icon); g_free (uri); g_key_file_free (key_file); if (icon) g_free (icon); if (name) g_free (name); if (comment) g_free (comment); if (path_freeme) g_free (path_freeme); }
/** * fm_app_info_launch * @appinfo: application info to launch * @files: (element-type GFile): files to use in run substitutions * @launch_context: (allow-none): a launch context * @error: (out) (allow-none): location to store error * * Launches desktop application doing substitutions in application info. * * Returns: %TRUE if application was launched. * * Since: 0.1.15 */ gboolean fm_app_info_launch(GAppInfo *appinfo, GList *files, GAppLaunchContext *launch_context, GError **error) { gboolean supported = FALSE, ret = FALSE; if(G_IS_DESKTOP_APP_INFO(appinfo)) { const char *id; #if GLIB_CHECK_VERSION(2,24,0) /* if GDesktopAppInfo knows the filename then let use it */ id = g_desktop_app_info_get_filename(G_DESKTOP_APP_INFO(appinfo)); if(id) /* this is a desktop entry file */ { /* load the desktop entry file to obtain more info */ GKeyFile* kf = g_key_file_new(); supported = g_key_file_load_from_file(kf, id, 0, NULL); if(supported) ret = do_launch(appinfo, id, kf, files, launch_context, error); g_key_file_free(kf); id = NULL; } else /* otherwise try application id */ #endif id = g_app_info_get_id(appinfo); if(id) /* this is an installed application */ { /* load the desktop entry file to obtain more info */ GKeyFile* kf = g_key_file_new(); char* rel_path = g_strconcat("applications/", id, NULL); char* full_desktop_path; supported = g_key_file_load_from_data_dirs(kf, rel_path, &full_desktop_path, 0, NULL); g_free(rel_path); if(supported) { ret = do_launch(appinfo, full_desktop_path, kf, files, launch_context, error); g_free(full_desktop_path); } g_key_file_free(kf); } else { #if GLIB_CHECK_VERSION(2,24,0) if (!supported) /* it was launched otherwise, see above */ #endif { /* If this is created with fm_app_info_create_from_commandline() */ if(g_object_get_data(G_OBJECT(appinfo), "flags")) { supported = TRUE; ret = do_launch(appinfo, NULL, NULL, files, launch_context, error); } } } } else supported = FALSE; if(!supported) /* fallback to GAppInfo::launch */ return g_app_info_launch(appinfo, files, launch_context, error); return ret; }
int main(int argc, char *argv[]) { char packet[] = {0x00, 0x00, 0x00}; float temp, hum; int error_count = 0; int count = 1; GError *error = NULL; GOptionContext *context; context = g_option_context_new ("- collect temperature and humidity readings"); g_option_context_add_main_entries (context, entries, NULL); if (!g_option_context_parse (context, &argc, &argv, &error)) { g_print ("option parsing failed: %s\n", error->message); exit (1); } g_option_context_free(context); set_debug(debug); hid_device *handle; gboolean found = search_for_device(); if(!found) { g_printerr("Sensor not found, aborting.\n"); exit(-1); } // Open the device using the VID and PID handle = open_lascar(); if(handle == NULL) { g_printerr("Error opening sensor.\n"); exit(-1); } SoupSession *session = NULL; int channel; gchar *channel_name; gchar *room; GString *uri, *body; /* parse config file */ GKeyFile *gkf = g_key_file_new(); g_key_file_load_from_data_dirs(gkf,"templogger.conf",NULL,G_KEY_FILE_NONE,&error); if(error!=NULL) { g_printerr("Can't load configuration file, not uploading to server.\n"); g_key_file_free(gkf); } else { gchar *url = g_key_file_get_string(gkf,"influx","url",NULL); channel = g_key_file_get_integer(gkf,"channel","channel_num",NULL); channel_name = g_key_file_get_string(gkf,"channel","channel_name",NULL); room = g_key_file_get_string(gkf,"channel","room",NULL); int port = g_key_file_get_integer(gkf,"influx","port",NULL); gchar *db = g_key_file_get_string(gkf,"influx","database",NULL); gchar *username = g_key_file_get_string(gkf,"influx","username",NULL); gchar *password = g_key_file_get_string(gkf,"influx","password",NULL); g_key_file_free(gkf); /* open session */ session = soup_session_new(); uri = g_string_new("http://"); g_string_append_printf(uri,"%s:%d/write?db=%s&u=%s&p=%s",url,port,db,username,password); g_message(uri->str); body = g_string_new(""); g_print("Uploading as channel %d, name %s, room %s",channel, channel_name, room); } FILE *logfile = NULL; if(log_local) { logfile = fopen("log.txt","a"); } const int upload_freq = floor(UPLOAD_TIME/SLEEP_TIME); while(1) { int ret = get_reading(handle, packet, &temp, &hum, TRUE); if(ret >= 0) { gint64 t = 1000*g_get_real_time(); if(log_local) { fprintf(logfile,"%" G_GINT64_FORMAT "\t%.1f\t%.1f\n",t, temp, hum); fflush(logfile); } if(debug) g_print("%" G_GINT64_FORMAT "\t%.1f\t%.1f\n",t, temp, hum); if(session && (count % upload_freq == 0)) { SoupRequestHTTP *request = soup_session_request_http(session,"POST",uri->str,NULL); SoupMessage *message = soup_request_http_get_message(request); g_string_append_printf(body,"temp,channel=%d,channel_name=%s,room=%s",channel,channel_name,room); g_string_append_printf(body," value=%.1f %" G_GINT64_FORMAT,temp,t); g_string_append_printf(body,"\n"); g_string_append_printf(body,"hum,channel=%d,channel_name=%s,room=%s",channel,channel_name,room); g_string_append_printf(body," value=%.1f %" G_GINT64_FORMAT,hum,t); g_string_append_printf(body,"\n"); if(debug) g_message(body->str); if(!testing) { soup_message_set_request(message,"application/binary",SOUP_MEMORY_COPY,body->str,body->len); guint session_status = soup_session_send_message(session,message); if(session_status == 204) { /* message was received */ //g_print("received status %d\n",session_status); g_string_erase(body,0,-1); /* clear the string */ } else { g_print("no connection to server"); } /* otherwise, keep it and attempt to resend next time */ } g_object_unref(message); } count ++; /* reset the error count on successful read */ error_count = 0; } else if(error_count > MAX_ERRORS) { g_printerr("Too many errors to continue\n"); exit(-1); } else { error_count++; } if(testing && count>60) break; g_usleep(SLEEP_TIME*1000000); } if(session) { g_string_free(uri,TRUE); g_string_free(body,TRUE); g_object_unref(session); } hid_close(handle); hid_exit(); return 0; }
/* read config from disk */ int read_config (window_board_t *win) { char rcfile[1024]; snprintf (rcfile, sizeof (rcfile), "%s/%s", g_get_user_config_dir (), "tenacerc"); if (! g_key_file_load_from_file (win->keyfile, rcfile, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL) && ! g_key_file_load_from_data_dirs (win->keyfile, "tenacerc", NULL, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL) ) { return 0; } /* Card display */ char *p; if ((p = g_key_file_get_string (win->keyfile, "tenace", "show_as", NULL))) { win->hand_display_style = !strcmp (p, "cards") ? HAND_DISPLAY_STYLE_CARDS : HAND_DISPLAY_STYLE_TEXT; } else { win->hand_display_style = HAND_DISPLAY_STYLE_CARDS; } if (win->svg) g_free (win->svg); if ((p = g_key_file_get_string (win->keyfile, "tenace", "svg", NULL))) { win->svg = p; } else { win->svg = NULL; } int i; if ((i = g_key_file_get_integer (win->keyfile, "tenace", "card_width", NULL))) { win->card_width = i; } if ((i = g_key_file_get_boolean (win->keyfile, "tenace", "show_played_cards", NULL))) { win->show_played_cards = i; } /* Hands */ if ((p = g_key_file_get_string (win->keyfile, "tenace", "show_hands", NULL))) { if (! strcmp (p, "none")) win->show_hands = seat_none; else if (! strcmp (p, "ns")) win->show_hands = north_south; else if (! strcmp (p, "ew")) win->show_hands = east_west; else win->show_hands = seat_all; } if ((p = g_key_file_get_string (win->keyfile, "tenace", "show_dd", NULL))) { if (! strcmp (p, "none")) win->show_dd_scores = seat_none; else if (! strcmp (p, "ns")) win->show_dd_scores = north_south; else if (! strcmp (p, "ew")) win->show_dd_scores = east_west; else win->show_dd_scores = seat_all; } return 1; }