xaizek / pms (License: GPLv3+) (since 2018-12-07)
Older version of Practical Music Search written in C++.
<root> / src / settings.cpp (3d9979b41b0773af20f09f764fdf522ee51eb5ec) (11KiB) (mode 100644) [raw]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
/* vi:set ts=8 sts=8 sw=8:
 *
 * PMS  <<Practical Music Search>>
 * Copyright (C) 2006-2015  Kim Tore Jensen
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * settings.cpp - configuration option class
 *
 */


#include "settings.h"
#include "config.h"
#include "pms.h"

using namespace std;

extern Pms *	pms;


/*
 * Constructors and destructors
 */
Setting::Setting()
{
	alias = NULL;
	type = SETTING_TYPE_EINVAL;
	key = "";
}

Options::Options()
{
	colors = NULL;
	reset();
}

Options::~Options()
{
	destroy();
}

void		Options::destroy()
{
	vector<Setting *>::iterator	i;

	/* Truncate old settings array */
	i = vals.begin();
	while (i != vals.end())
	{
		delete *i;
		++i;
	}
	vals.clear();

	/* Truncate topbar */
	clear_topbar();

	if (colors != NULL)
		delete colors;
}


/*
 * Reset to defaults
 */
void		Options::reset()
{
	destroy();

	colors = new Colortable();

	set_string("host", "localhost");
	set_long("port", 6600);
	set_string("password", "");

	set("scroll", SETTING_TYPE_SCROLL, "normal");
	set("columns", SETTING_TYPE_FIELDLIST, "artist track title album length");
	set("sort", SETTING_TYPE_FIELDLIST, "track disc album date albumartistsort");

	set_long("nextinterval", 5);
	set_long("crossfade", 5);
	set_long("mpd_timeout", 30);
	set_long("stopdelay", 1);
	set_long("reconnectdelay", 30);
	set_long("directoryminlen", 30);
	set_long("resetstatus", 3);
	set_long("scrolloff", 0);
	set_long("msg_buffer_size", 1024);

	set_bool("debug", false);
	set_bool("addtoreturns", false);
	set_bool("ignorecase", true);
	set_bool("regexsearch", false);
	set_bool("followwindow", false);
	set_bool("followcursor", false);
	set_bool("followplayback", false);
	set_bool("nextafteraction", true);
	set_bool("topbarclear", false);
	set_bool("showtopbar", true);
	set_bool("topbarborders", false);
	set_bool("topbarspace", true);
	set_bool("columnspace", true);
	set_bool("columnborders", false);
	set_bool("mouse", false);

	set_string("directoryformat", "%artist% – %title%");
	set_string("xtermtitle",  "PMS: %playstate%%ifcursong% %artist% – %title%%endif%");
	set_string("onplaylistfinish", "");
	set_string("libraryroot", "");
	set_string("startuplist", "playlist");
	set_string("albumclass", "artist album date"); //FIXME: implement this

	set_string("status_unknown", Pms::unicode() ? "?" : "??");
	set_string("status_play", Pms::unicode() ? "▶" : "|>");
	set_string("status_pause", Pms::unicode() ? "‖" : "||");
	set_string("status_stop", Pms::unicode() ? "■" : "[]");

	/*
	 * Set up option aliases
	 */
	alias("ic", "ignorecase");
	alias("so", "scrolloff");

	/* Set up default top bar values */
	topbar.clear();
	while(topbar.size() < 3)
		topbar.push_back(new Topbarline());

	topbar[0]->strings[0] = _("%time_elapsed% %playstate% %time%%ifcursong% (%progresspercentage%%%)%endif%");
	topbar[0]->strings[1] = _("%ifcursong%%artist%%endif%");
	topbar[0]->strings[2] = _("Vol: %volume%%%  Mode: %muteshort%%consumeshort%%repeatshort%%randomshort%%singleshort%");
	topbar[1]->strings[1] = _("%ifcursong%==> %title% <==%else%No current song%endif%");
	topbar[2]->strings[0] = _("%listsize%");
	topbar[2]->strings[1] = _("%ifcursong%%album% (%year%)%endif%");
	topbar[2]->strings[2] = _("Q: %livequeuesize%");
}


/*
 * Find a setting based on keyword
 */
Setting *	Options::lookup(string key)
{
	unsigned int	i;

	for (i = 0; i < vals.size(); i++)
	{
		if (vals[i]->key == key)
			return vals[i];
	}

	return NULL;
}


/*
 * Initialize a setting or return an existing one with type t.
 */
Setting *	Options::add(string key, SettingType t)
{
	Setting *	s;

	s = lookup(key);
	if (s != NULL)
		return s;

	s = new Setting();
	if (s == NULL)
		return s;
	
	s->key = key;
	s->type = t;

	vals.push_back(s);

	return s;
}


/*
 * Alias a keyword to point to another keyword.
 * Limitations: needs to be added _after_ the original keyword.
 * Can be nested indefinately.
 */
bool		Options::alias(string key, string dest)
{
	Setting *	s_key;
	Setting *	s_dest;

	s_dest = lookup(dest);
	if (s_dest == NULL)
		return false;
	
	s_key = add(key, SETTING_TYPE_ALIAS);
	if (s_key == NULL)
		return false;
	
	s_key->alias = s_dest;

	return true;
}


/*
 * Set a variable, make best guess.
 * This function expects options to already exist. Use it from the Configurator class.
 */
bool		Options::set(string key, string val)
{
	Setting *	s;

	pms->log(MSG_DEBUG, 0, "set: Setting option '%s'='%s'\n", key.c_str(), val.c_str());
	pms->msg->clear();

	if (key.size() > 6 && key.substr(0, 6) == "topbar")
	{
		if (set_topbar_values(key, val))
		{
			set_string(key, val);
			pms->mediator->add("setting.topbar");
			return true;
		}
	}

	s = lookup(key);

	if (s == NULL)
	{
		pms->msg->code = CERR_INVALID_OPTION;
		pms->msg->str = _("invalid option");
		pms->msg->str += " '" + key + "'";
		return false;
	}

	while (s->alias != NULL)
		s = s->alias;

	if (set(s->key, s->type, val) != NULL)
	{
		pms->mediator->add("setting." + key);
		return true;
	}
	else if (pms->msg->code == CERR_NONE)
	{
		pms->msg->code = CERR_INVALID_VALUE;
		pms->msg->str = _("invalid value");
		pms->msg->str += " '" + val + "' ";
		pms->msg->str += _("for option");
		pms->msg->str += " '" + s->key + "'";
	}
	return false;
}

/*
 * Set a special or generic variable.
 * This function does all type error checking and converts strings to other types.
 */
Setting *	Options::set(string key, SettingType t, string val)
{
	Setting *	s;

//	fprintf(stderr, "set(%s, %d, %s)\n", key.c_str(), t, val.c_str());

	s = add(key, t);
	if (s == NULL || s->type != t)
		return NULL;

	/*
	 * Special case
	 */
	if (key == "topbarclear")
	{
		clear_topbar();
		return s;
	}

	switch(t)
	{
		case SETTING_TYPE_STRING:
			return set_string(key, val);

		case SETTING_TYPE_LONG:
			return set_long(key, atoi(val.c_str()));

		case SETTING_TYPE_BOOLEAN:
			return set_bool(key, Configurator::strtobool(val));

		case SETTING_TYPE_FIELDLIST:
			if (Configurator::verify_columns(val))
				s->v_string = val;
			else
				return NULL;
			break;

		case SETTING_TYPE_SCROLL:
			if (val == "centered" || val == "centred")
				set_long(key, SCROLL_CENTERED);
			else if (val == "relative")
				set_long(key, SCROLL_RELATIVE);
			else if (val == "normal")
				set_long(key, SCROLL_NORMAL);
			else
			{
				pms->msg->clear();
				pms->msg->code = CERR_INVALID_VALUE;
				pms->msg->str = _("invalid scroll mode, expected 'normal', 'centered' or 'relative'");
				return NULL;
			}
			s->v_string = val;
			break;

		default:
			s->v_string = val;
			s->v_long = 0;
			s->v_bool = 0;
	}

	return s;
}

/*
 * Set a string variable
 */
Setting *	Options::set_string(string key, string val)
{
	Setting *	s;

	s = add(key, SETTING_TYPE_STRING);
	if (s != NULL)
		s->v_string = val;

	return s;
}

/*
 * Set a numeric variable
 */
Setting *	Options::set_long(string key, long val)
{
	Setting *	s;

	s = add(key, SETTING_TYPE_LONG);
	if (s != NULL)
		s->v_long = val;

	return s;
}

/*
 * Set a boolean variable
 */
Setting *	Options::set_bool(string key, bool val)
{
	Setting *	s;

	s = add(key, SETTING_TYPE_BOOLEAN);
	if (s != NULL)
		s->v_bool = val;

	return s;
}

/*
 * Toggle a boolean variable
 */
bool		Options::toggle(string key)
{
	Setting *	s;

	s = lookup(key);
	if (s == NULL)
		return false;
	else if (s->type != SETTING_TYPE_BOOLEAN)
		return false;
	
	s->v_bool = !(s->v_bool);
	return true;
}

/*
 * Return the type of a variable
 */
SettingType	Options::get_type(string key)
{
	Setting *	s;

	s = lookup(key);
	if (s == NULL)
		return SETTING_TYPE_EINVAL;

	while (s->alias != NULL)
		s = s->alias;

	return s->type;
}


/*
 * Read functions
 */
string		Options::get_string(string key)
{
	Setting *	s;

	s = lookup(key);
	if (s == NULL)
		return "";

	while (s->alias != NULL)
		s = s->alias;
	
	return s->v_string;
}

long		Options::get_long(string key)
{
	Setting *	s;

	s = lookup(key);
	if (s == NULL)
		return 0;
	
	while (s->alias != NULL)
		s = s->alias;
	
	return s->v_long;
}

bool		Options::get_bool(string key)
{
	Setting *	s;

	s = lookup(key);
	if (s == NULL)
		return false;
	
	while (s->alias != NULL)
		s = s->alias;
	
	return s->v_bool;
}


/*
 * Dump an option
 */
bool		Options::dump(string key)
{
	Setting *	s;

	pms->msg->clear();
	s = lookup(key);

	if (s == NULL)
	{
		pms->msg->code = CERR_INVALID_OPTION;
		pms->msg->str = _("invalid option");
		pms->msg->str += " '" + key + "'";
		return false;
	}
	else
	{
		while (s->alias != NULL)
			s = s->alias;
		pms->msg->code = CERR_NONE;
		pms->msg->str = dump(s);
		return true;
	}
}

string		Options::dump(Setting * s)
{
	string		r = "";

	if (s == NULL)
		return r;

	switch(s->type)
	{
		default:
		case SETTING_TYPE_STRING:
			r = s->key + "=";
			r += s->v_string;
			break;

		case SETTING_TYPE_LONG:
			r = s->key + "=";
			r += Pms::tostring(s->v_long);
			break;

		case SETTING_TYPE_BOOLEAN:
			if (!(s->v_bool))
				r = "no";
			r += s->key;
			break;
	}

	return r;
}

/*
 * Dump all options to a long string
 */
string		Options::dump_all()
{
	string		output = "";

	unsigned int	i;
	Setting *	s;

	for (i = 0; i < vals.size(); i++)
	{
		s = vals[i];
		output += "set ";
		output += dump(s);
		output += "\n";
	}

	return output;
}

/*
 * Set which fields to show in the topbar.
 */
bool		Options::set_topbar_values(string name, string value)
{
	int		column, row;

	column = atoi(name.substr(6).c_str());

	if (column <= 0)
		return false;
	else if (column < 10)
		name = name.substr(7);
	else if (column < 100)
		name = name.substr(8);
	else
	{
		pms->msg->clear();
		pms->msg->code = CERR_INVALID_TOPBAR_INDEX;
		pms->msg->str = _("invalid topbar line");
		pms->msg->str += " '" + Pms::tostring(column) + "', ";
		pms->msg->str += _("expected range is 1-99");
		return false;
	}

	if (name.size() == 0)
	{
		pms->msg->clear();
		pms->msg->code = CERR_INVALID_TOPBAR_POSITION;
		pms->msg->str = _("expected placement after topbar index");
		return false;
	}

	if (name == ".left")
		row = 0;
	else if (name == ".center" || name == ".centre")
		row = 1;
	else if (name == ".right")
		row = 2;
	else
	{
		pms->msg->clear();
		pms->msg->code = CERR_INVALID_TOPBAR_POSITION;
		pms->msg->str = _("invalid topbar position");
		pms->msg->str += " '" + name.substr(1) + "', ";
		pms->msg->str += _("expected one of: left center right");
		return false;
	}

	/* Arguments are now sanitized, should have row = 0-2, and column = 1-99 */

	while (topbar.size() < column)
		topbar.push_back(new Topbarline());

	topbar[column-1]->strings[row] = value;

	return true;
}


/*
 * Clear the topbar
 */
void		Options::clear_topbar()
{
	vector<Topbarline *>::iterator	i;

	i = topbar.begin();

	while (i != topbar.end())
	{
		delete *i;
		++i;
	}

	topbar.clear();
}
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/pms

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/pms

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master