Move file deletion from Writer to Call
[skype-call-recorder.git] / preferences.cpp
blob6a0de5a35141581490b505958948cef31ffcc436
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 #include <QVBoxLayout>
25 #include <QHBoxLayout>
26 #include <QGroupBox>
27 #include <QLabel>
28 #include <QPushButton>
29 #include <QListView>
30 #include <QFile>
31 //#include <QSet>
32 #include <QTextStream>
33 #include <QtAlgorithms>
35 #include "preferences.h"
36 #include "smartwidgets.h"
37 #include "common.h"
39 Preferences preferences;
41 // preferences dialog
43 static QVBoxLayout *makeVFrame(QVBoxLayout *parentLayout, const char *title) {
44 QGroupBox *box = new QGroupBox(title);
45 QVBoxLayout *vbox = new QVBoxLayout(box);
46 parentLayout->addWidget(box);
47 return vbox;
50 static QHBoxLayout *makeHFrame(QVBoxLayout *parentLayout, const char *title) {
51 QGroupBox *box = new QGroupBox(title);
52 QHBoxLayout *hbox = new QHBoxLayout(box);
53 parentLayout->addWidget(box);
54 return hbox;
57 PreferencesDialog::PreferencesDialog() {
58 setWindowTitle(PROGRAM_NAME " - Preferences");
60 QVBoxLayout *vbox;
61 QHBoxLayout *hbox;
62 QLabel *label;
63 QPushButton *button;
64 SmartComboBox *combo;
65 SmartEditableComboBox *ecombo;
66 SmartLineEdit *edit;
67 SmartRadioButton *radio;
68 SmartCheckBox *check;
70 QVBoxLayout *bigvbox = new QVBoxLayout(this);
71 bigvbox->setSizeConstraint(QLayout::SetFixedSize);
73 // ---- general options ----
74 hbox = makeHFrame(bigvbox, "Automatic recording");
76 vbox = new QVBoxLayout;
77 Preference &preference = preferences.get("autorecord.default");
78 radio = new SmartRadioButton("&Automatically record calls", preference, "yes");
79 radio->setEnabled(false);
80 vbox->addWidget(radio);
81 radio = new SmartRadioButton("Ask every time", preference, "ask");
82 radio->setEnabled(false);
83 vbox->addWidget(radio);
84 radio = new SmartRadioButton("Do not automatically record calls", preference, "no");
85 radio->setEnabled(false);
86 vbox->addWidget(radio);
88 hbox->addLayout(vbox);
90 button = new QPushButton("Per-caller preferences");
91 connect(button, SIGNAL(clicked(bool)), this, SLOT(editPerCallerSettings()));
92 button->setDisabled(true);
93 hbox->addWidget(button, 0, Qt::AlignBottom);
95 // ---- output file name ----
96 vbox = makeVFrame(bigvbox, "Output file");
98 label = new QLabel("&Save recorded calls here:");
99 edit = new SmartLineEdit(preferences.get("output.path"));
100 label->setBuddy(edit);
101 vbox->addWidget(label);
102 vbox->addWidget(edit);
104 label = new QLabel("File &name:");
105 ecombo = new SmartEditableComboBox(preferences.get("output.pattern"));
106 label->setBuddy(ecombo);
107 ecombo->addItem("%Y, %B/Skype call with &s, %A %B %d, %Y, %H:%M:%S");
108 ecombo->addItem("Calls with &s/Skype call with &s, %A %B %d, %Y, %H:%M:%S");
109 ecombo->addItem("Calls with &s/Skype call with &s, %A %B %d, %Y, %I:%M:%S%p");
110 ecombo->addItem("Skype call with &s, %A, %B %d, %Y, %H:%M:%S");
111 ecombo->addItem("%Y-%m-%d %H:%M:%S call with &s");
112 ecombo->setupDone();
113 vbox->addWidget(label);
114 vbox->addWidget(ecombo);
116 label = new QLabel("Example: blah blah");
117 vbox->addWidget(label);
119 // ---- output file format ----
120 vbox = makeVFrame(bigvbox, "Output file &format");
122 hbox = new QHBoxLayout;
124 formatWidget = combo = new SmartComboBox(preferences.get("output.format"));
125 combo->addItem("WAV PCM", "wav");
126 combo->addItem("MP3", "mp3");
127 combo->setupDone();
128 connect(combo, SIGNAL(currentIndexChanged(int)), this, SLOT(enableMp3Settings()));
129 hbox->addWidget(combo);
131 combo = new SmartComboBox(preferences.get("output.format.mp3.bitrate"));
132 combo->addItem("8 kbps", 8);
133 combo->addItem("16 kbps", 16);
134 combo->addItem("24 kbps", 24);
135 combo->addItem("32 kbps", 32);
136 combo->addItem("40 kbps", 40);
137 combo->addItem("48 kbps", 48);
138 combo->addItem("56 kbps", 56);
139 combo->addItem("64 kbps", 64);
140 combo->addItem("80 kbps", 80);
141 combo->addItem("96 kbps", 96);
142 combo->addItem("112 kbps", 112);
143 combo->addItem("128 kbps", 128);
144 combo->addItem("144 kbps", 144);
145 combo->addItem("160 kbps", 160);
146 combo->setupDone();
147 mp3Settings.append(combo);
148 hbox->addWidget(combo);
150 combo = new SmartComboBox(preferences.get("output.channelmode"));
151 combo->addItem("Mix to mono", "mono");
152 combo->addItem("Stereo, local left, remote right", "stereo");
153 combo->addItem("Stereo, local right, remote left", "oerets");
154 combo->setupDone();
155 hbox->addWidget(combo);
157 vbox->addLayout(hbox);
159 check = new SmartCheckBox("Save call &information in MP3 files", preferences.get("output.savetags"));
160 //mp3Settings.append(check);
161 check->setEnabled(false);
162 vbox->addWidget(check);
164 // ---- buttons ----
166 hbox = new QHBoxLayout;
167 button = new QPushButton("&Close");
168 button->setDefault(true);
169 connect(button, SIGNAL(clicked(bool)), this, SLOT(accept()));
170 hbox->addStretch();
171 hbox->addWidget(button);
172 bigvbox->addLayout(hbox);
174 enableMp3Settings();
177 void PreferencesDialog::enableMp3Settings() {
178 QVariant v = formatWidget->itemData(formatWidget->currentIndex());
179 bool b = v == "mp3";
180 for (int i = 0; i < mp3Settings.size(); i++)
181 mp3Settings.at(i)->setEnabled(b);
184 void PreferencesDialog::editPerCallerSettings() {
185 new PerCallerSettingsDialog(this);
188 // per caller preferences editor
190 PerCallerSettingsDialog::PerCallerSettingsDialog(QWidget *parent) : QDialog(parent) {
191 setWindowTitle("Per-caller Preferences");
192 setWindowModality(Qt::WindowModal);
193 setAttribute(Qt::WA_DeleteOnClose);
195 QVBoxLayout *vbox = new QVBoxLayout(this);
197 list = new QListView;
198 //QStringList l;
199 //l.append("Skype name");
200 //l.append("Record automatically");
201 //table->setHorizontalHeaderLabels(l);
202 //table->setSelectionMode(QAbstractItemView::ExtendedSelection);
203 //table->setSelectionBehavior(QAbstractItemView::SelectRows);
204 //table->setTextElideMode(Qt::ElideRight);
205 //table->setSortingEnabled(true);
206 //table->setGridStyle(Qt::NoPen);
207 //table->setShowGrid(false);
208 vbox->addWidget(list);
210 QHBoxLayout *hbox = new QHBoxLayout;
212 QPushButton *button = new QPushButton("&Add");
213 connect(button, SIGNAL(clicked(bool)), this, SLOT(add()));
214 hbox->addWidget(button);
216 button = new QPushButton("&Remove");
217 connect(button, SIGNAL(clicked(bool)), this, SLOT(remove()));
218 hbox->addWidget(button);
220 button = new QPushButton("&Close");
221 button->setDefault(true);
222 connect(button, SIGNAL(clicked(bool)), this, SLOT(accept()));
223 hbox->addWidget(button);
225 vbox->addLayout(hbox);
227 // fill in data
229 QSet<QString> seen;
230 int rows = 0;
232 l = preferences.get("autorecord.yes").toList();
233 for (int i = 0; i < l.count(); i++) {
234 if (seen.contains(l.at(i)))
235 continue;
236 table->setRowCount(rows + 1);
237 table->setItem(rows, 0, new QTableWidgetItem(l.at(i)));
238 table->setItem(rows, 1, new QTableWidgetItem("yes"));
239 rows++;
242 l = preferences.get("autorecord.ask").toList();
243 for (int i = 0; i < l.count(); i++) {
244 if (seen.contains(l.at(i)))
245 continue;
246 table->setRowCount(rows + 1);
247 table->setItem(rows, 0, new QTableWidgetItem(l.at(i)));
248 table->setItem(rows, 1, new QTableWidgetItem("ask"));
249 rows++;
252 l = preferences.get("autorecord.no").toList();
253 for (int i = 0; i < l.count(); i++) {
254 if (seen.contains(l.at(i)))
255 continue;
256 table->setRowCount(rows + 1);
257 table->setItem(rows, 0, new QTableWidgetItem(l.at(i)));
258 table->setItem(rows, 1, new QTableWidgetItem("no"));
259 rows++;
262 //list->sortItems();
264 //for (int i = 0; i < list->count(); i++) {
265 // QListWidgetItem *item = list->item(i);
266 // item->setFlags(item->flags() | Qt::ItemIsEditable);
269 connect(this, SIGNAL(finished(int)), this, SLOT(saveSetting()));
271 show();
274 void PerCallerSettingsDialog::add() {
275 //QListWidgetItem *item = new QListWidgetItem("Enter skype name");
276 //item->setFlags(item->flags() | Qt::ItemIsEditable);
277 //int i = list->currentRow();
278 //if (i < 0)
279 // list->addItem(item);
280 //else
281 // list->insertItem(i + 1, item);
283 //list->clearSelection();
284 //list->setCurrentItem(item);
285 //list->editItem(item);
288 void PerCallerSettingsDialog::remove() {
289 //QList<QListWidgetItem *> sel = list->selectedItems();
290 //while (!sel.isEmpty())
291 // delete sel.takeFirst();
294 void PerCallerSettingsDialog::saveSetting() {
295 //table->sortItems(0);
296 //QSet<QString> set;
297 //for (int i = 0; i < list->count(); i++)
298 // set.insert(list->item(i)->text());
299 //preference.set(set.toList());
302 // preferences
304 bool Preferences::load(const QString &filename) {
305 clear();
306 QFile file(filename);
307 if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
308 debug(QString("Can't open '%1' for loading preferences").arg(filename));
309 return false;
311 char buf[65536];
312 while (!file.atEnd()) {
313 qint64 len = file.readLine(buf, sizeof(buf));
314 if (len == -1)
315 break;
316 QString line(buf);
317 line = line.trimmed();
318 if (line.at(0) == '#')
319 continue;
320 int index = line.indexOf('=');
321 if (index < 0)
322 // TODO warn
323 continue;
324 get(line.left(index).trimmed()).set(line.mid(index + 1).trimmed());
326 debug(QString("Loaded %1 preferences from '%2'").arg(preferences.size()).arg(filename));
327 return true;
330 bool Preferences::save(const QString &filename) {
331 qSort(preferences);
332 QFile file(filename);
333 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
334 debug(QString("Can't open '%1' for saving preferences").arg(filename));
335 return false;
337 QTextStream out(&file);
338 for (int i = 0; i < preferences.size(); i++) {
339 const Preference &p = preferences.at(i);
340 out << p.name() << " = " << p.toString() << "\n";
342 debug(QString("Saved %1 preferences to '%2'").arg(preferences.size()).arg(filename));
343 return true;
346 Preference &Preferences::get(const QString &name) {
347 for (int i = 0; i < preferences.size(); i++)
348 if (preferences.at(i).name() == name)
349 return preferences[i];
350 preferences.append(Preference(name));
351 return preferences.last();