Fix bug with calls on hold
[skype-call-recorder.git] / recorder.cpp
blob7c390dda63c87640a211a6986800640521844f89
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 <iostream>
26 #include <QMessageBox>
27 #include <QTextEdit>
28 #include <QDir>
29 #include <QProcess>
30 #include <QTimer>
31 #include <cstdlib>
33 #include "recorder.h"
34 #include "common.h"
35 #include "about.h"
36 #include "trayicon.h"
37 #include "preferences.h"
38 #include "skype.h"
39 #include "call.h"
41 Recorder::Recorder(int argc, char **argv) :
42 QApplication(argc, argv),
43 preferencesDialog(NULL)
45 recorderInstance = this;
47 debug("Initializing application");
49 // check for already running instance
50 if (!lockFile.lock(QDir::homePath() + "/.skypecallrecorder.lock")) {
51 debug("Other instance is running");
52 QTimer::singleShot(0, this, SLOT(quit()));
53 return;
56 loadPreferences();
58 setupGUI();
59 setupSkype();
60 setupCallHandler();
63 Recorder::~Recorder() {
64 delete preferencesDialog;
67 void Recorder::setupGUI() {
68 setWindowIcon(QIcon(":/icon.png"));
69 setQuitOnLastWindowClosed(false);
71 trayIcon = new TrayIcon(this);
72 connect(trayIcon, SIGNAL(requestQuit()), this, SLOT(quitConfirmation()));
73 connect(trayIcon, SIGNAL(requestQuitNoConfirmation()), this, SLOT(quit()));
74 connect(trayIcon, SIGNAL(requestAbout()), this, SLOT(about()));
75 connect(trayIcon, SIGNAL(requestOpenPreferences()), this, SLOT(openPreferences()));
76 connect(trayIcon, SIGNAL(requestBrowseCalls()), this, SLOT(browseCalls()));
78 preferencesDialog = new PreferencesDialog();
79 connect(preferencesDialog, SIGNAL(finished(int)), this, SLOT(savePreferences()));
81 debug("GUI initialized");
84 void Recorder::setupSkype() {
85 skype = new Skype(this);
86 connect(skype, SIGNAL(notify(const QString &)), this, SLOT(skypeNotify(const QString &)));
87 connect(skype, SIGNAL(connected(bool)), this, SLOT(skypeConnected(bool)));
88 connect(skype, SIGNAL(connectionFailed(const QString &)), this, SLOT(skypeConnectionFailed(const QString &)));
90 connect(skype, SIGNAL(connected(bool)), trayIcon, SLOT(setColor(bool)));
93 void Recorder::setupCallHandler() {
94 callHandler = new CallHandler(this, skype);
96 connect(trayIcon, SIGNAL(startRecording(int)), callHandler, SLOT(startRecording(int)));
97 connect(trayIcon, SIGNAL(stopRecording(int)), callHandler, SLOT(stopRecording(int)));
98 connect(trayIcon, SIGNAL(stopRecordingAndDelete(int)), callHandler, SLOT(stopRecordingAndDelete(int)));
100 connect(callHandler, SIGNAL(startedCall(int, const QString &)), trayIcon, SLOT(startedCall(int, const QString &)));
101 connect(callHandler, SIGNAL(stoppedCall(int)), trayIcon, SLOT(stoppedCall(int)));
102 connect(callHandler, SIGNAL(startedRecording(int)), trayIcon, SLOT(startedRecording(int)));
103 connect(callHandler, SIGNAL(stoppedRecording(int)), trayIcon, SLOT(stoppedRecording(int)));
106 QString Recorder::getConfigFile() const {
107 return QDir::homePath() + "/.skypecallrecorder.rc";
110 void Recorder::loadPreferences() {
111 preferences.load(getConfigFile());
112 int c = preferences.count();
114 #define X(n, v) preferences.get(#n).setIfNotSet(v);
115 // default preferences
116 X(autorecord.default, "ask"); // "yes", "ask", "no"
117 X(autorecord.ask, ""); // comma separated skypenames to always ask for
118 X(autorecord.yes, ""); // comma separated skypenames to always record
119 X(autorecord.no, ""); // comma separated skypenames to never record
120 X(output.path, "~/Skype Calls");
121 X(output.pattern, "Calls with &s/Call with &s, %a %b %d %Y, %H:%M:%S");
122 X(output.format, "mp3"); // "mp3" or "wav"
123 X(output.format.mp3.bitrate, 96);
124 X(output.channelmode, "stereo"); // mono, stereo, oerets
125 X(output.savetags, true);
126 X(suppress.legalinformation, false);
127 #undef X
129 c = preferences.count() - c;
131 if (c)
132 debug(QString("Loading %1 built-in default preference(s)").arg(c));
135 void Recorder::savePreferences() {
136 preferences.save(getConfigFile());
137 // TODO: when failure?
140 void Recorder::about() {
141 if (!aboutDialog)
142 aboutDialog = new AboutDialog;
144 aboutDialog->raise();
145 aboutDialog->activateWindow();
148 void Recorder::openPreferences() {
149 debug("Show preferences dialog");
150 preferencesDialog->show();
151 preferencesDialog->raise();
152 preferencesDialog->activateWindow();
155 void Recorder::closePreferences() {
156 debug("Hide preferences dialog");
157 preferencesDialog->hide();
160 void Recorder::browseCalls() {
161 QString program;
162 QStringList arguments;
164 const char *v = std::getenv("GNOME_DESKTOP_SESSION_ID");
165 if (v && *v) {
166 // GNOME is running
167 program = "gnome-open";
168 } else {
169 // otherwise, just launch kfmclient. KDE could be detected via
170 // KDE_FULL_SESSION=true
171 program = "kfmclient";
172 arguments << "exec";
175 QString path = getOutputPath();
176 QDir().mkpath(path);
177 arguments << path;
178 int ret = QProcess::execute(program, arguments);
180 if (ret != 0) {
181 QMessageBox::information(NULL, PROGRAM_NAME, QString("Failed to launch '%1 %2', exit code %3").
182 arg(program, arguments.join(" ")).arg(ret));
186 void Recorder::quitConfirmation() {
187 debug("Request to quit");
188 savePreferences();
189 quit();
192 void Recorder::skypeNotify(const QString &s) {
193 QStringList args = s.split(' ');
194 QString cmd = args.takeFirst();
195 if (cmd == "CALL")
196 callHandler->callCmd(args);
199 void Recorder::skypeConnected(bool conn) {
200 if (conn)
201 debug("skype connection established");
202 else
203 debug("skype not connected");
206 void Recorder::skypeConnectionFailed(const QString &reason) {
207 debug("skype connection failed, reason: " + reason);
209 QMessageBox::critical(NULL, PROGRAM_NAME " - Error",
210 QString("The connection to Skype failed! %1 cannot operate without this "
211 "connection, please make sure you haven't blocked access from within Skype.\n\n"
212 "Internal reason for failure: %2").arg(PROGRAM_NAME, reason));
215 void Recorder::debugMessage(const QString &s) {
216 std::cout << s.toLocal8Bit().constData() << "\n";
219 int main(int argc, char **argv) {
220 Recorder recorder(argc, argv);
222 return recorder.exec();