File: prefs.cpp

package info (click to toggle)
madman 0.93.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,060 kB
  • ctags: 3,309
  • sloc: cpp: 14,194; ansic: 13,331; sh: 100; makefile: 62; python: 54
file content (208 lines) | stat: -rw-r--r-- 8,324 bytes parent folder | download
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
/*
madman - a music manager
Copyright (C) 2003  Andreas Kloeckner <ak@ixion.net>

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 2 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, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/




#include <qsettings.h>

#include <stdexcept>

#include "utility/player.h"
#include "utility/plugin.h"
#include "utility/prefs.h"
#include "database/song.h"




namespace
{
  void addAutoDJScoringRule(const QString &comment, const QString &criterion, int score, tAutoDJPreferences &prefs)
  {
    tAutoDJScoringRule adj;
    adj.Comment = comment;
    adj.Criterion = criterion;
    adj.ScoreAdjustment = score;
    prefs.ScoringRuleList.push_back(adj);
  }
}




// tAutoDJPreferences ---------------------------------------------------------
void tAutoDJPreferences::resetToDefault()
{
  ScoringRuleList.clear();
  addAutoDJScoringRule("Baseline score", "~all", 5, *this);
  addAutoDJScoringRule("Unrated", "~unrated", 5, *this);
  addAutoDJScoringRule("Rated '*****'", "~rating(5)", 15, *this);
  addAutoDJScoringRule("Rated '****'", "~rating(4)", 12, *this);
  addAutoDJScoringRule("Rated '***'", "~rating(3)", 8, *this);
  addAutoDJScoringRule("Rated '**'", "~rating(2)", 5, *this);
  addAutoDJScoringRule("Rated '*'", "~rating(1)", 3, *this);
  addAutoDJScoringRule("Rated '-'", "~rating(0)", -10, *this);
  addAutoDJScoringRule("Unplayed", "~play_count(0)", 4, *this);
  addAutoDJScoringRule("Played within an hour", "~last_played_n_days_ago(<=0.04166)", -10, *this);
  addAutoDJScoringRule("Played within 24 hours", "~last_played_n_days_ago(<=1)&~last_played_n_days_ago(>0.04166)", -5, *this);
  addAutoDJScoringRule("Played within 3 days", "~last_played_n_days_ago(<=3)&~last_played_n_days_ago(>1)", -3, *this);
  addAutoDJScoringRule("Played within a week", "~last_played_n_days_ago(<=7)&~last_played_n_days_ago(>3)", -1, *this);
  addAutoDJScoringRule("Not played for a month", "~last_played_n_days_ago(>=30)&~last_played_n_days_ago(<60)", 3, *this);
  addAutoDJScoringRule("Not played for two months", "!~last_played_n_days_ago(>60)", 6, *this);
  addAutoDJScoringRule("Often played to the end", "~play_count(>=4)&~full_play_ratio(>0.5)", 5, *this);
  addAutoDJScoringRule("Rarely played to the end", "~play_count(>=4)&~full_play_ratio(<0.3)", -5, *this);
  addAutoDJScoringRule("Played more than three times", "~play_count(>=3)", 3, *this);
  addAutoDJScoringRule("Less than a week old", "~existed_for_days(<= 7)", 10, *this);
  addAutoDJScoringRule("Less than two weeks old", "~existed_for_days(<= 14)", 5, *this);
  addAutoDJScoringRule("Less than three weeks old", "~existed_for_days(<= 21)", 3, *this);
}




// tPreferences ---------------------------------------------------------------
void tPreferences::save(QSettings &settings)
{
  settings.writeEntry("/madman/player", Player.name());

#define WRITE_BOOL(SETTING, VARIABLE) \
  settings.writeEntry("/madman/" SETTING, VARIABLE ? 1 : 0);

  QStringList plugin_dirs = stringvector2QStringList(PluginDirectories);
  settings.writeEntry("/madman/plugin_directories", plugin_dirs);
  WRITE_BOOL("remember_geometry", RememberGeometry);
  WRITE_BOOL("scan_at_startup", ScanAtStartup);
  WRITE_BOOL("collect_history", CollectHistory);
  WRITE_BOOL("enable_tray_icon", EnableSystemTrayIcon);
  WRITE_BOOL("enable_system_tray_icon", EnableSystemTrayIcon);
  WRITE_BOOL("minimize_to_system_tray", MinimizeToSystemTray);
  WRITE_BOOL("enable_passive_popup_song_announcements", EnablePassivePopupSongAnnouncements);

  settings.writeEntry("/madman/number_of_backups_kept", BackupCount);

  WRITE_BOOL("http_daemon_enabled", HttpDaemonEnabled);
  settings.writeEntry("/madman/http_server_port", HttpDaemonPort);
  WRITE_BOOL("http_downloads_enabled", HttpDownloadsEnabled);
  WRITE_BOOL("http_scripting_enabled", HttpScriptingEnabled);
  WRITE_BOOL("http_write_scripting_enabled", HttpWriteScriptingEnabled);
  WRITE_BOOL("http_local_play_enabled", HttpLocalPlayEnabled);
  WRITE_BOOL("http_browsing_enabled", HttpBrowsingEnabled);
  WRITE_BOOL("http_restrict_to_localhost", HttpRestrictToLocalhost);

  settings.writeEntry("/madman/tray_tooltip_format", TrayTooltipFormat );
  settings.writeEntry("/madman/passive_popup_format", PassivePopupFormat);

  unsigned i = 0;
  settings.writeEntry("/madman/auto_dj/score_adjustment_count", (int) AutoDJPreferences.ScoringRuleList.size());
  FOREACH_CONST(first, AutoDJPreferences.ScoringRuleList, tAutoDJScoringRuleList)
  {
    const tAutoDJScoringRule &adj(*first);
    settings.writeEntry(QString("/madman/auto_dj/score_adjustment.%1.comment").arg(i), 
	adj.Comment);
    settings.writeEntry(QString("/madman/auto_dj/score_adjustment.%1.criterion").arg(i), 
	adj.Criterion);
    settings.writeEntry(QString("/madman/auto_dj/score_adjustment.%1.score").arg(i), 
	adj.ScoreAdjustment);
    i++;
  }
}




void tPreferences::load(QSettings &settings)
{
  QString player_name = settings.readEntry("/madman/player");
  
  tPlayer * new_player = createPlayer(player_name);
  if (!new_player)
  {
    vector<QString> players;
    listPlayers(players);
    if (players.size())
      new_player = createPlayer(players[0]);
    if (!new_player)
      throw runtime_error("can't instantiate any player, giving up.");
  }
  setPlayer(new_player);

  bool ok;
  PluginDirectories = QStringList2stringvector(settings.readListEntry("/madman/plugin_directories", &ok));
  if (!ok)
  {
    PluginDirectories.clear();
    PluginDirectories.push_back(STRINGIFY(MADMAN_LIBDIR) "/madman/plugins");
  }
#define READ_BOOL(SETTING, VARIABLE, DEFAULT) \
  VARIABLE = settings.readNumEntry("/madman/" SETTING, DEFAULT);

  READ_BOOL("remember_geometry", RememberGeometry, 1);
  READ_BOOL("scan_at_startup", ScanAtStartup, 0);
  READ_BOOL("collect_history", CollectHistory, 1);
  READ_BOOL("enable_system_tray_icon", EnableSystemTrayIcon, 1);
  READ_BOOL("minimize_to_system_tray", MinimizeToSystemTray, 0);
  READ_BOOL("enable_passive_popup_song_announcements", EnablePassivePopupSongAnnouncements, 1);

  BackupCount = settings.readNumEntry("/madman/number_of_backups_kept", 4);

  READ_BOOL("http_daemon_enabled", HttpDaemonEnabled, 0);
  HttpDaemonPort = settings.readNumEntry("/madman/http_server_port", 51533);
  READ_BOOL("http_downloads_enabled", HttpDownloadsEnabled, 1);
  READ_BOOL("http_scripting_enabled", HttpScriptingEnabled, 1);
  READ_BOOL("http_write_scripting_enabled", HttpWriteScriptingEnabled, 1);
  READ_BOOL("http_local_play_enabled", HttpLocalPlayEnabled, 1);
  READ_BOOL("http_browsing_enabled", HttpBrowsingEnabled, 1);
  READ_BOOL("http_restrict_to_localhost", HttpRestrictToLocalhost, 1);

  TrayTooltipFormat = settings.readEntry("/madman/tray_tooltip_format", "madman: %artist% - %title%");
  PassivePopupFormat = settings.readEntry("/madman/passive_popup_format", "%artist%%newline%%title%");

  unsigned criteria_count = 
    settings.readNumEntry("/madman/auto_dj/score_adjustment_count", 0);
  if (criteria_count == 0)
    AutoDJPreferences.resetToDefault();
  else
  {
    for (unsigned i = 0; i < criteria_count; i++)
    {
      tAutoDJScoringRule adj;
      adj.Comment = settings.readEntry(
	  QString("/madman/auto_dj/score_adjustment.%1.comment").arg(i), "");
      adj.Criterion = settings.readEntry(
	  QString("/madman/auto_dj/score_adjustment.%1.criterion").arg(i), "");
      adj.ScoreAdjustment = settings.readNumEntry(
	  QString("/madman/auto_dj/score_adjustment.%1.score").arg(i), 0);

      if (adj.ScoreAdjustment < adj.ScorePlayNever)
	adj.ScoreAdjustment = adj.ScorePlayNever;
      if (adj.ScoreAdjustment > adj.ScoreMaximum)
	adj.ScoreAdjustment = adj.ScoreMaximum;
      AutoDJPreferences.ScoringRuleList.push_back(adj);
    }
  }
}




void tPreferences::setPlayer(tPlayer *newplayer)
{
  Player.setBackend(newplayer);
}