Replace strings representing preference names with constants
[skype-call-recorder.git] / preferences.h
blob15f93a68472c2775a482a33725f9d43ff47245cf
1 /*
2 Skype Call Recorder
3 Copyright (C) 2008 jlh (jlh at gmx dot ch)
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2 of the License, version 3 of
8 the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 The GNU General Public License version 2 is included with the source of
20 this program under the file name COPYING. You can also get a copy on
21 http://www.fsf.org/
24 #ifndef PREFERENCES_H
25 #define PREFERENCES_H
27 #include <QDialog>
28 #include <QList>
29 #include <QString>
30 #include <QStringList>
31 #include <QAbstractListModel>
33 #include "common.h"
35 class SmartComboBox;
36 class QListView;
37 class PerCallerModel;
38 class PerCallerPreferencesDialog;
39 class QRadioButton;
40 class SmartEditableComboBox;
41 class QDateTime;
43 // A single preference, with a name and a value
45 class Preference {
46 public:
47 Preference(const Preference &p) : m_name(p.m_name), isSet(p.isSet), value(p.value) { }
48 Preference(const QString &n) : m_name(n), isSet(false) { }
49 template <typename T> Preference(const QString &n, const T &t) : m_name(n), isSet(false) { set(t); }
51 const QString &toString() const { return value; }
52 int toInt() const { return value.toInt(); }
53 bool toBool() const { return value.compare("yes", Qt::CaseInsensitive) == 0 || value.toInt(); }
54 QStringList toList() const { return value.split(',', QString::SkipEmptyParts); }
55 void listAdd(const QString &);
56 void listRemove(const QString &);
57 bool listContains(const QString &);
59 void set(const char *v) { isSet = true; value = v; }
60 void set(const QString &v) { isSet = true; value = v; }
61 void set(int v) { isSet = true; value.setNum(v); }
62 void set(bool v) { isSet = true; value = v ? "yes" : "no"; }
63 void set(const QStringList &v) { isSet = true; value = v.join(","); }
65 template <typename T> void setIfNotSet(const T &v) { if (!isSet) set(v); }
67 const QString &name() const { return m_name; }
69 bool operator<(const Preference &rhs) const {
70 return m_name < rhs.m_name;
73 private:
74 QString m_name;
75 bool isSet;
76 QString value;
78 private:
79 // disable assignment. we want preference names to be immutable.
80 Preference &operator=(const Preference &);
83 // A collection of preferences that can be loaded/saved
85 class BasePreferences {
86 public:
87 BasePreferences() { };
88 ~BasePreferences();
90 bool load(const QString &);
91 bool save(const QString &);
93 Preference &get(const QString &);
94 void clear();
96 int count() const { return prefs.size(); }
98 private:
99 // this is a list of pointers, so we can control the life time of each
100 // Preference. we want references to them to be valid forever. Only
101 // QLinkedList could give that guarantee, but it's unpractical for
102 // sorting. QList<Preference> would technically be implemented as an
103 // array of pointers too, but its sorting semantics with regard to
104 // references are not the one we want.
105 QList<Preference *> prefs;
107 DISABLE_COPY_AND_ASSIGNMENT(BasePreferences);
110 // preferences with some utils
112 class Preferences : public BasePreferences {
113 public:
114 Preferences() { };
116 void setPerCallerPreference(const QString &, int);
118 DISABLE_COPY_AND_ASSIGNMENT(Preferences);
121 // The preferences dialog
123 class PreferencesDialog : public QDialog {
124 Q_OBJECT
125 public:
126 PreferencesDialog();
128 protected:
129 void hideEvent(QHideEvent *);
131 private slots:
132 void updateFormatSettings();
133 void editPerCallerPreferences();
134 void perCallerFinished();
135 void updatePatternToolTip(const QString &);
137 private:
138 QList<QWidget *> mp3Settings;
139 QList<QWidget *> vorbisSettings;
140 SmartComboBox *formatWidget;
141 PerCallerPreferencesDialog *perCallerDialog;
142 SmartEditableComboBox *patternWidget;
144 DISABLE_COPY_AND_ASSIGNMENT(PreferencesDialog);
147 // The per caller editor dialog
149 class PerCallerPreferencesDialog : public QDialog {
150 Q_OBJECT
151 public:
152 PerCallerPreferencesDialog(QWidget *);
154 private slots:
155 void add(const QString & = QString(), int = 1, bool = true);
156 void remove();
157 void selectionChanged();
158 void radioChanged();
159 void save();
161 private:
162 QListView *listWidget;
163 PerCallerModel *model;
164 QRadioButton *radioYes;
165 QRadioButton *radioAsk;
166 QRadioButton *radioNo;
168 DISABLE_COPY_AND_ASSIGNMENT(PerCallerPreferencesDialog);
171 // per caller model
173 class PerCallerModel : public QAbstractListModel {
174 Q_OBJECT
175 public:
176 PerCallerModel(QObject *parent) : QAbstractListModel(parent) { }
177 int rowCount(const QModelIndex & = QModelIndex()) const;
178 QVariant data(const QModelIndex &, int) const;
179 bool setData(const QModelIndex &, const QVariant &, int = Qt::EditRole);
180 bool insertRows(int, int, const QModelIndex &);
181 bool removeRows(int, int, const QModelIndex &);
182 void sort(int = 0, Qt::SortOrder = Qt::AscendingOrder);
183 Qt::ItemFlags flags(const QModelIndex &) const;
185 private:
186 QStringList skypeNames;
187 QList<int> modes;
190 // the only instance of Preferences
192 extern Preferences preferences;
193 extern QString getOutputPath();
194 extern QString getFileName(const QString &, const QString &, const QString &,
195 const QString &, const QDateTime &, const QString & = QString());
197 // preference constants
199 #define X(name, string) const char * const name = #string;
201 namespace Pref {
203 X(AutoRecordDefault, autorecord.default)
204 X(AutoRecordAsk, autorecord.ask)
205 X(AutoRecordYes, autorecord.yes)
206 X(AutoRecordNo, autorecord.no)
207 X(OutputPath, output.path)
208 X(OutputPattern, output.pattern)
209 X(OutputFormat, output.format)
210 X(OutputFormatMp3Bitrate, output.format.mp3.bitrate)
211 X(OutputFormatVorbisQuality, output.format.vorbis.quality)
212 X(OutputChannelMode, output.channelmode)
213 X(OutputSaveTags, output.savetags)
214 X(SuppressLegalInformation, suppress.legalinformation)
215 X(SuppressFirstRunInformation, suppress.firstruninformation)
219 #undef X
221 #endif