Пример #1
0
/*
* Remove a device
*/
int Settings::removeNode(Node type, int intNodeId) {
	TelldusCore::MutexLocker locker(&mutex);
	FILE *fp = fopen(CONFIG_FILE, "we");  // e for setting O_CLOEXEC on the file handle
	if (!fp) {
		return TELLSTICK_ERROR_PERMISSION_DENIED;
	}

	std::string strType = getNodeString(type);

	// Print all opts
	for(int i = 0; d->cfg->opts[i].name; i++) {
		// Check if it isn't a device section
		if (strcmp(d->cfg->opts[i].name, strType.c_str()) != 0) {
			cfg_opt_print(&d->cfg->opts[i], fp);
		} else {
			// Print all sections except the one to remove
			cfg_t *cfg_node;
			for (int i = 0; i < cfg_size(d->cfg, strType.c_str()); ++i) {
				cfg_node = cfg_getnsec(d->cfg, strType.c_str(), i);
				if (cfg_getint(cfg_node, "id") != intNodeId) {  // This isn't the one to skip
					fprintf(fp, "%s {\n", strType.c_str());
					cfg_print_indent(cfg_node, fp, 1);
					fprintf(fp, "}\n");
				}
			}
		}
	}
	fclose(fp);

	// Re-read config-file
	cfg_free(d->cfg);
	readConfig(&d->cfg);

	return TELLSTICK_SUCCESS;
}
Пример #2
0
void
gimmix_config_save (Conf *conf)
{
	FILE *fp;
	cfg_t *cfg;
	cfg_opt_t *sopts;
	
	cfg_opt_t opts[] = {
		CFG_SIMPLE_STR ("mpd_hostname", NULL),
		CFG_SIMPLE_INT ("mpd_port", 0),
		CFG_SIMPLE_STR ("mpd_password", NULL),
		CFG_SIMPLE_BOOL ("enable_systray", false),
		CFG_SIMPLE_BOOL ("enable_notify", false),
		CFG_END()
	};

	cfg = cfg_init(opts, 0);
	char *rcfile = cfg_tilde_expand ("~/.gimmixrc");
	
	if((fp = fopen(rcfile, "w")))
	{	
		fprintf (fp, "# Gimmix configuration\n");
		fprintf (fp, "\n# MPD hostname (default: localhost)\n");
		if (conf->hostname)
			cfg_setstr(cfg, "mpd_hostname", conf->hostname);
		sopts = cfg_getopt (cfg, "mpd_hostname");
		cfg_opt_print (sopts, fp);

		fprintf (fp, "\n# MPD port (default: 6600)\n");
		if (conf->port > 0)
			cfg_setint(cfg, "mpd_port", conf->port);
		else
			cfg_setint(cfg, "mpd_port", 0);
		sopts = cfg_getopt (cfg, "mpd_port");
		cfg_opt_print (sopts, fp);
		
		fprintf (fp, "\n# MPD password (leave blank for no password) \n");
		if (conf->password)
			cfg_setstr(cfg, "mpd_password", conf->password);
		sopts = cfg_getopt (cfg, "mpd_password");
		cfg_opt_print (sopts, fp);

		fprintf (fp, "\n# Enable/Disable systray icon (Enable = true, Disable = false)\n");
		if (conf->systray_enable == 1)
			cfg_setbool(cfg, "enable_systray", true);
		else
			cfg_setbool(cfg, "enable_systray", false);
		sopts = cfg_getopt (cfg, "enable_systray");
		cfg_opt_print (sopts, fp);
		
		fprintf (fp, "\n# Enable/Disable system tray notifications (Enable = true, Disable = false) \n");
		if (conf->notify_enable == 1)
			cfg_setbool(cfg, "enable_notify", true);
		else
			cfg_setbool(cfg, "enable_notify", false);
		sopts = cfg_getopt (cfg, "enable_notify");
		cfg_opt_print (sopts, fp);

		free (rcfile);
		fclose (fp);
	}
	else
	{	
		fprintf (stderr, "Error while saving config.\n");
	}

	cfg_free_value (opts);
	cfg_free (cfg);
	
	return;
}