Fix bug about preferences messing up
[skype-call-recorder.git] / recorder.cpp
blobea2ed75c8387cee5e92dab7c1e98944398d448ee
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 <QMessageBox>
25 #include <QDir>
26 #include <QProcess>
27 #include <QTimer>
28 #include <iostream>
29 #include <cstdlib>
31 #include "recorder.h"
32 #include "common.h"
33 #include "gui.h"
34 #include "trayicon.h"
35 #include "preferences.h"
36 #include "skype.h"
37 #include "call.h"
39 Recorder::Recorder(int argc, char **argv) :
40 QApplication(argc, argv),
41 preferencesDialog(NULL)
43 recorderInstance = this;
45 debug("Initializing application");
47 // check for already running instance
48 if (!lockFile.lock(QDir::homePath() + "/.skypecallrecorder.lock")) {
49 debug("Other instance is running");
50 QTimer::singleShot(0, this, SLOT(quit()));
51 return;
54 loadPreferences();
56 setupGUI();
57 setupSkype();
58 setupCallHandler();
61 Recorder::~Recorder() {
62 delete preferencesDialog;
65 void Recorder::setupGUI() {
66 setWindowIcon(QIcon(":/icon.png"));
67 setQuitOnLastWindowClosed(false);
69 trayIcon = new TrayIcon(this);
70 connect(trayIcon, SIGNAL(requestQuit()), this, SLOT(quitConfirmation()));
71 connect(trayIcon, SIGNAL(requestQuitNoConfirmation()), this, SLOT(quit()));
72 connect(trayIcon, SIGNAL(requestAbout()), this, SLOT(about()));
73 connect(trayIcon, SIGNAL(requestOpenPreferences()), this, SLOT(openPreferences()));
74 connect(trayIcon, SIGNAL(requestBrowseCalls()), this, SLOT(browseCalls()));
76 preferencesDialog = new PreferencesDialog();
77 connect(preferencesDialog, SIGNAL(finished(int)), this, SLOT(savePreferences()));
79 debug("GUI initialized");
81 if (!preferences.get("suppress.firstruninformation").toBool())
82 new FirstRunDialog();
85 void Recorder::setupSkype() {
86 skype = new Skype(this);
87 connect(skype, SIGNAL(notify(const QString &)), this, SLOT(skypeNotify(const QString &)));
88 connect(skype, SIGNAL(connected(bool)), this, SLOT(skypeConnected(bool)));
89 connect(skype, SIGNAL(connectionFailed(const QString &)), this, SLOT(skypeConnectionFailed(const QString &)));
91 connect(skype, SIGNAL(connected(bool)), trayIcon, SLOT(setColor(bool)));
94 void Recorder::setupCallHandler() {
95 callHandler = new CallHandler(this, skype);
97 connect(trayIcon, SIGNAL(startRecording(int)), callHandler, SLOT(startRecording(int)));
98 connect(trayIcon, SIGNAL(stopRecording(int)), callHandler, SLOT(stopRecording(int)));
99 connect(trayIcon, SIGNAL(stopRecordingAndDelete(int)), callHandler, SLOT(stopRecordingAndDelete(int)));
101 connect(callHandler, SIGNAL(startedCall(int, const QString &)), trayIcon, SLOT(startedCall(int, const QString &)));
102 connect(callHandler, SIGNAL(stoppedCall(int)), trayIcon, SLOT(stoppedCall(int)));
103 connect(callHandler, SIGNAL(startedRecording(int)), trayIcon, SLOT(startedRecording(int)));
104 connect(callHandler, SIGNAL(stoppedRecording(int)), trayIcon, SLOT(stoppedRecording(int)));
107 QString Recorder::getConfigFile() const {
108 return QDir::homePath() + "/.skypecallrecorder.rc";
111 void Recorder::loadPreferences() {
112 preferences.load(getConfigFile());
113 int c = preferences.count();
115 #define X(n, v) preferences.get(#n).setIfNotSet(v);
116 // default preferences
117 X(autorecord.default, "ask"); // "yes", "ask", "no"
118 X(autorecord.ask, ""); // comma separated skypenames to always ask for
119 X(autorecord.yes, ""); // comma separated skypenames to always record
120 X(autorecord.no, ""); // comma separated skypenames to never record
121 X(output.path, "~/Skype Calls");
122 X(output.pattern, "Calls with &s/Call with &s, %a %b %d %Y, %H:%M:%S");
123 X(output.format, "mp3"); // "mp3" or "wav"
124 X(output.format.mp3.bitrate, 64);
125 X(output.format.vorbis.quality, 3);
126 X(output.channelmode, "stereo"); // mono, stereo, oerets
127 X(output.savetags, true);
128 X(suppress.legalinformation, false);
129 X(suppress.firstruninformation, false);
130 #undef X
132 c = preferences.count() - c;
134 if (c)
135 debug(QString("Loading %1 built-in default preference(s)").arg(c));
138 void Recorder::savePreferences() {
139 preferences.save(getConfigFile());
140 // TODO: when failure?
143 void Recorder::about() {
144 if (!aboutDialog)
145 aboutDialog = new AboutDialog;
147 aboutDialog->raise();
148 aboutDialog->activateWindow();
151 void Recorder::openPreferences() {
152 debug("Show preferences dialog");
153 preferencesDialog->show();
154 preferencesDialog->raise();
155 preferencesDialog->activateWindow();
158 void Recorder::closePreferences() {
159 debug("Hide preferences dialog");
160 preferencesDialog->hide();
163 void Recorder::browseCalls() {
164 QString program;
165 QStringList arguments;
167 const char *v = std::getenv("GNOME_DESKTOP_SESSION_ID");
168 if (v && *v) {
169 // GNOME is running
170 program = "gnome-open";
171 } else {
172 // otherwise, just launch kfmclient. KDE could be detected via
173 // KDE_FULL_SESSION=true
174 program = "kfmclient";
175 arguments << "exec";
178 QString path = getOutputPath();
179 QDir().mkpath(path);
180 arguments << path;
181 int ret = QProcess::execute(program, arguments);
183 if (ret != 0) {
184 QMessageBox::information(NULL, PROGRAM_NAME, QString("Failed to launch '%1 %2', exit code %3").
185 arg(program, arguments.join(" ")).arg(ret));
189 void Recorder::quitConfirmation() {
190 debug("Request to quit");
191 savePreferences();
192 quit();
195 void Recorder::skypeNotify(const QString &s) {
196 QStringList args = s.split(' ');
197 QString cmd = args.takeFirst();
198 if (cmd == "CALL")
199 callHandler->callCmd(args);
202 void Recorder::skypeConnected(bool conn) {
203 if (conn)
204 debug("skype connection established");
205 else
206 debug("skype not connected");
209 void Recorder::skypeConnectionFailed(const QString &reason) {
210 debug("skype connection failed, reason: " + reason);
212 QMessageBox::critical(NULL, PROGRAM_NAME " - Error",
213 QString("The connection to Skype failed! %1 cannot operate without this "
214 "connection, please make sure you haven't blocked access from within Skype.\n\n"
215 "Internal reason for failure: %2").arg(PROGRAM_NAME, reason));
218 void Recorder::debugMessage(const QString &s) {
219 std::cout << s.toLocal8Bit().constData() << "\n";
222 int main(int argc, char **argv) {
223 Recorder recorder(argc, argv);
225 return recorder.exec();