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
|
/***************************************************************************
* Copyright (C) 2006 by Ilya Kotov *
* forkotov02@hotmail.ru *
* *
* 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 <QApplication>
#include <QTcpSocket>
#include <unistd.h>
#include "mainwindow.h"
#include "version.h"
#include "qmmpstarter.h"
#include "guard.h"
QMMPStarter::QMMPStarter(int argc,char ** argv,QObject* parent) : QObject(parent),mw(0)
{
QStringList tmp;
for(int i = 1;i < argc;i++)
tmp << QString::fromLocal8Bit(argv[i]);
argString = tmp.join("\n");
if(argString == "--help")
{
printUsage();
exit(0);
}
else if(argString == "--version")
{
printVersion();
exit(0);
}
if(argString.startsWith("--") && // command?
argString != "--play" &&
argString != "--previous" &&
argString != "--next" &&
argString != "--stop" &&
argString != "--pause" &&
argString != "--play-pause"
)
{
qFatal("QMMP: Unknown command...");
exit(1);
}
if(Guard::exists(QApplication::applicationFilePath()))
{
m_tcpSocket = new QTcpSocket(this);
connect(m_tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(m_tcpSocket, SIGNAL(connected()),this, SLOT(writeCommand()));
m_tcpSocket->connectToHost("127.0.0.1",TCPSERVER_PORT_NUMBER + getuid());
}
else
{
Guard::create(QApplication::applicationFilePath());
QStringList arg_l = argString.split("\n", QString::SkipEmptyParts);
mw = new MainWindow(arg_l,0);
}
}
void QMMPStarter::displayError(QAbstractSocket::SocketError socketError)
{
switch (socketError)
{
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
qWarning("The host was not found");
break;
case QAbstractSocket::ConnectionRefusedError:
qWarning("The connection was refused by the peer. ");
break;
default:
qWarning("The following error: %s:",qPrintable(m_tcpSocket->errorString()));
}
Guard::create(QApplication::applicationFilePath());
mw = new MainWindow(argString.split("\n", QString::SkipEmptyParts),0);
}
QMMPStarter::~ QMMPStarter()
{
if(mw)
{
Guard::destroy(QApplication::applicationFilePath());
delete mw;
}
}
void QMMPStarter::writeCommand()
{
if(!argString.isEmpty())
{
char buf[PATH_MAX + 1];
QString workingDir = QString(getcwd(buf,PATH_MAX)) + "\n";
QByteArray barray;
barray.append(workingDir);
barray.append(argString);
m_tcpSocket->write(barray);
m_tcpSocket->flush();
}
else
{
qWarning("It seems that another version of application is already running ...\n");
printUsage();
}
m_tcpSocket->close();
QApplication::quit();
}
void QMMPStarter::printUsage()
{
qWarning(
"Usage: qmmp [options] [files] \n"
"Options:\n"
"--------\n"
"--help Display this text and exit.\n"
"--previous Skip backwards in playlist\n"
"--play Start playing current playlist\n"
"--pause Pause current song\n"
"--play-pause Pause if playing, play otherwise\n"
"--stop Stop current song\n"
"--next Skip forward in playlist\n"
"--version Print version number and exit.\n\n"
"Ideas, patches, bugreports send to forkotov02@hotmail.ru\n"
);
}
void QMMPStarter::printVersion()
{
qWarning("QMMP version: %s",QMMP_STR_VERSION);
}
|