├── TODO
├── README
├── src
├── qtsingleapplication
│ ├── QtLockedFile
│ ├── QtSingleApplication
│ ├── qtsinglecoreapplication.h
│ ├── qtlocalpeer.h
│ ├── qtlockedfile.h
│ ├── qtlockedfile_unix.cpp
│ ├── qtsingleapplication.h
│ ├── qtsinglecoreapplication.cpp
│ ├── qtlockedfile.cpp
│ ├── qtlockedfile_win.cpp
│ ├── qtlocalpeer.cpp
│ └── qtsingleapplication.cpp
├── init.h
├── contact.h
├── uuid.h
├── sec_req.h
├── statusnotify.h
├── cookie.h
├── scan.h
├── globaldeclarations.h
├── send_msg.h
├── httpget.h
├── httppost.h
├── modcontact.h
├── contact.cpp
├── src.pri
├── httpget.cpp
├── sec_req.cpp
├── init.cpp
├── httppost.cpp
├── statusnotify.cpp
├── userobject.h
├── uuid.cpp
├── send_msg.cpp
├── main.cpp
├── cookie.cpp
├── scan.cpp
└── modcontact.cpp
├── resources.qrc
├── TODO.md
├── .gitignore
├── qml
├── main.qml
├── ContactListView.qml
└── LoginView.qml
├── qwx.pro
├── README.md
└── LICENSE
/TODO:
--------------------------------------------------------------------------------
1 | TODO.md
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | README.md
--------------------------------------------------------------------------------
/src/qtsingleapplication/QtLockedFile:
--------------------------------------------------------------------------------
1 | #include "qtlockedfile.h"
2 |
--------------------------------------------------------------------------------
/src/qtsingleapplication/QtSingleApplication:
--------------------------------------------------------------------------------
1 | #include "qtsingleapplication.h"
2 |
--------------------------------------------------------------------------------
/resources.qrc:
--------------------------------------------------------------------------------
1 |
2 |
3 | qml/main.qml
4 | qml/LoginView.qml
5 | qml/ContactListView.qml
6 |
7 |
8 |
--------------------------------------------------------------------------------
/TODO.md:
--------------------------------------------------------------------------------
1 | - [ ] Cache uin, sid, skey to ~/.qwx/cache I know it is UNSAFE ;P but people
2 | does not need to re-scan QRcode time and time again!
3 | - [ ] Reverse engineering weixin.apk to take place of scanning QRcode.
4 |
5 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | moc_*
2 | qrc_*
3 | Makefile
4 |
5 | # Compiled Object files
6 | *.slo
7 | *.lo
8 | *.o
9 | *.obj
10 |
11 | # Precompiled Headers
12 | *.gch
13 | *.pch
14 |
15 | # Compiled Dynamic libraries
16 | *.so
17 | *.dylib
18 | *.dll
19 |
20 | # Fortran module files
21 | *.mod
22 |
23 | # Compiled Static libraries
24 | *.lai
25 | *.la
26 | *.a
27 | *.lib
28 |
29 | # Executables
30 | *.exe
31 | *.out
32 | *.app
33 |
--------------------------------------------------------------------------------
/qml/main.qml:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | import QtQuick 2.2
4 | import QtQuick.Controls 1.1
5 | import QtQuick.Window 2.1
6 |
7 | ApplicationWindow {
8 | id: rootWindow
9 | width: 310; height: 500
10 |
11 | StackView {
12 | id: stackView
13 | width: parent.width; height: parent.height
14 | anchors.fill: parent
15 | initialItem: LoginView {}
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/init.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef INIT_H
4 | #define INIT_H
5 |
6 | #include "httppost.h"
7 |
8 | class Init : public HttpPost
9 | {
10 | Q_OBJECT
11 |
12 | public:
13 | Init(HttpPost* parent = nullptr);
14 | ~Init();
15 |
16 | public:
17 | Q_INVOKABLE void post(QString uin, QString sid, QString skey);
18 |
19 | protected:
20 | void finished(QNetworkReply* reply);
21 | };
22 |
23 | #endif // INIT_H
24 |
--------------------------------------------------------------------------------
/src/contact.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef CONTACT_H
4 | #define CONTACT_H
5 |
6 | #include "httppost.h"
7 |
8 | class Contact : public HttpPost
9 | {
10 | Q_OBJECT
11 |
12 | public:
13 | Contact(HttpPost* parent = nullptr);
14 | ~Contact();
15 |
16 | Q_INVOKABLE void post();
17 |
18 | Q_SIGNALS:
19 | void contactChanged();
20 |
21 | protected:
22 | void finished(QNetworkReply* reply);
23 | };
24 |
25 | #endif // CONTACT_H
26 |
--------------------------------------------------------------------------------
/src/uuid.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef UUID_H
4 | #define UUID_H
5 |
6 | #include "httpget.h"
7 |
8 | class UUID : public HttpGet
9 | {
10 | Q_OBJECT
11 |
12 | public:
13 | UUID(HttpGet* parent = nullptr);
14 | ~UUID();
15 |
16 | Q_INVOKABLE void get();
17 |
18 | Q_SIGNALS:
19 | void error();
20 | void uuidChanged(QString uuid);
21 |
22 | protected:
23 | void finished(QNetworkReply* reply);
24 | };
25 |
26 | #endif // UUID_H
27 |
--------------------------------------------------------------------------------
/src/sec_req.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef SEC_REQ_H
4 | #define SEC_REQ_H
5 |
6 | #include "httppost.h"
7 |
8 | // Second Request, indeed I am not sure weixin still use it ;P
9 | class SecReq : public HttpPost
10 | {
11 | Q_OBJECT
12 |
13 | public:
14 | SecReq(HttpPost* parent = nullptr);
15 | ~SecReq();
16 |
17 | public:
18 | Q_INVOKABLE void post(QString uuid);
19 |
20 | protected:
21 | void finished(QNetworkReply* reply);
22 | };
23 |
24 | #endif // SEC_REQ_H
25 |
--------------------------------------------------------------------------------
/src/statusnotify.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef STATUS_NOTIFY_H
4 | #define STATUS_NOTIFY_H
5 |
6 | #include "httppost.h"
7 |
8 | class StatusNotify : public HttpPost
9 | {
10 | Q_OBJECT
11 |
12 | public:
13 | StatusNotify(HttpPost* parent = nullptr);
14 | ~StatusNotify();
15 |
16 | public:
17 | Q_INVOKABLE void post(QString uin, QString sid, QString skey, QString userName);
18 |
19 | protected:
20 | void finished(QNetworkReply* reply);
21 | };
22 |
23 | #endif // STATUS_NOTIFY_H
24 |
--------------------------------------------------------------------------------
/src/cookie.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef COOKIE_H
4 | #define COOKIE_H
5 |
6 | #include "httpget.h"
7 |
8 | class Cookie : public HttpGet
9 | {
10 | Q_OBJECT
11 |
12 | public:
13 | Cookie(HttpGet* parent = nullptr);
14 | ~Cookie();
15 |
16 | Q_INVOKABLE void get(QString redirect_uri);
17 |
18 | Q_SIGNALS:
19 | void infoChanged(QString skey, QString sid, QString uin, QString ticket);
20 |
21 | protected:
22 | void finished(QNetworkReply* reply);
23 | };
24 |
25 | #endif // COOKIE_H
26 |
--------------------------------------------------------------------------------
/src/scan.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef SCAN_H
4 | #define SCAN_H
5 |
6 | #include "httpget.h"
7 |
8 | class Scan : public HttpGet
9 | {
10 | Q_OBJECT
11 |
12 | public:
13 | Scan(HttpGet* parent = nullptr);
14 | ~Scan();
15 |
16 | Q_INVOKABLE void get(QString uuid);
17 |
18 | Q_SIGNALS:
19 | void error(QString strerror);
20 | void scanedButWaitConfirm();
21 | void scanedAndConfirmed(QString redirect_uri);
22 |
23 | protected:
24 | void finished(QNetworkReply* reply);
25 | };
26 |
27 | #endif // SCAN_H
28 |
--------------------------------------------------------------------------------
/src/globaldeclarations.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 2014 Leslie Zhai
2 |
3 | #ifndef GLOBAL_DECLARATIONS_H
4 | #define GLOBAL_DECLARATIONS_H
5 |
6 | #include
7 |
8 | const QString CODE_NAME = "qwx";
9 | const QString APPLICATION_VERSION = "0.1";
10 | const QString APPLICATION_ENCODING = "UTF-8";
11 |
12 | const QString LOGIN_SERVER_HOST = "https://login.weixin.qq.com";
13 | const QString WX_SERVER_HOST = "https://wx.qq.com";
14 | const QString WX_CGI_PATH = "/cgi-bin/mmwebwx-bin/";
15 | const QString DEVICE_ID = "e519062714508114";
16 |
17 | #endif // GLOBAL_DECLARATIONS_H
18 |
--------------------------------------------------------------------------------
/src/send_msg.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef SEND_MSG_H
4 | #define SEND_MSG_H
5 |
6 | #include "httppost.h"
7 |
8 | class SendMsg : public HttpPost
9 | {
10 | Q_OBJECT
11 |
12 | public:
13 | SendMsg(HttpPost* parent = nullptr);
14 | ~SendMsg();
15 |
16 | public:
17 | Q_INVOKABLE void post(QString uin,
18 | QString sid,
19 | QString skey,
20 | QString fromUserName,
21 | QString toUserName,
22 | QString content);
23 |
24 | protected:
25 | void finished(QNetworkReply* reply);
26 | };
27 |
28 | #endif // SEND_MSG_H
29 |
--------------------------------------------------------------------------------
/src/httpget.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef HTTP_GET_H
4 | #define HTTP_GET_H
5 |
6 | #include
7 | #include
8 | #include
9 |
10 | class HttpGet : public QObject
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | HttpGet(QObject* parent = nullptr);
16 | virtual ~HttpGet();
17 |
18 | public:
19 | void get(QString url);
20 |
21 | protected:
22 | virtual void finished(QNetworkReply* reply);
23 |
24 | private Q_SLOTS:
25 | void m_finished(QNetworkReply* reply);
26 | void m_sslErrors(QNetworkReply* reply, const QList & errors);
27 |
28 | private:
29 | QNetworkAccessManager m_nam;
30 | };
31 |
32 | #endif // HTTP_GET_H
33 |
--------------------------------------------------------------------------------
/src/httppost.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef HTTP_POST_H
4 | #define HTTP_POST_H
5 |
6 | #include
7 | #include
8 | #include
9 |
10 | class HttpPost : public QObject
11 | {
12 | Q_OBJECT
13 |
14 | public:
15 | HttpPost(QObject* parent = nullptr);
16 | virtual ~HttpPost();
17 |
18 | public:
19 | void post(QString url, QString str);
20 |
21 | protected:
22 | virtual void finished(QNetworkReply* reply);
23 |
24 | private Q_SLOTS:
25 | void m_finished(QNetworkReply* reply);
26 | void m_sslErrors(QNetworkReply* reply, const QList & errors);
27 |
28 | private:
29 | QNetworkAccessManager m_nam;
30 | };
31 |
32 | #endif // HTTP_POST_H
33 |
--------------------------------------------------------------------------------
/src/modcontact.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef MOD_CONTACT_H
4 | #define MOD_CONTACT_H
5 |
6 | #include "httppost.h"
7 |
8 | class ModContact : public HttpPost
9 | {
10 | Q_OBJECT
11 |
12 | Q_PROPERTY(QList modContactList READ modContactList NOTIFY modContactListChanged)
13 |
14 | public:
15 | ModContact(HttpPost* parent = nullptr);
16 | ~ModContact();
17 |
18 | QList modContactList() const;
19 |
20 | Q_INVOKABLE void post(QString uin, QString sid);
21 |
22 | Q_SIGNALS:
23 | void error();
24 | void modContactListChanged();
25 |
26 | protected:
27 | void finished(QNetworkReply* reply);
28 |
29 | private:
30 | QList m_modContactList;
31 | };
32 |
33 | #endif // MOD_CONTACT_H
34 |
--------------------------------------------------------------------------------
/src/contact.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #include "contact.h"
4 | #include "globaldeclarations.h"
5 |
6 | Contact::Contact(HttpPost* parent)
7 | : HttpPost(parent)
8 | {
9 | #if QWX_DEBUG
10 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
11 | #endif
12 | }
13 |
14 | Contact::~Contact()
15 | {
16 | #if QWX_DEBUG
17 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
18 | #endif
19 | }
20 |
21 | void Contact::post()
22 | {
23 | QString url = WX_SERVER_HOST + WX_CGI_PATH + "webwxgetcontact?r=" +
24 | QString::number(time(NULL));
25 | #if QWX_DEBUG
26 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
27 | #endif
28 | QString json = "{}";
29 | HttpPost::post(url, json);
30 | }
31 |
32 | void Contact::finished(QNetworkReply* reply)
33 | {
34 | QString replyStr(reply->readAll());
35 | #if QWX_DEBUG
36 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
37 | qDebug() << "DEBUG:" << replyStr;
38 | #endif
39 | }
40 |
--------------------------------------------------------------------------------
/src/src.pri:
--------------------------------------------------------------------------------
1 | HEADERS += \
2 | $$PWD/globaldeclarations.h \
3 | $$PWD/qtsingleapplication/qtsingleapplication.h \
4 | $$PWD/qtsingleapplication/qtlocalpeer.h \
5 | $$PWD/httpget.h \
6 | $$PWD/uuid.h \
7 | $$PWD/scan.h \
8 | $$PWD/cookie.h \
9 | $$PWD/httppost.h \
10 | $$PWD/sec_req.h \
11 | $$PWD/init.h \
12 | $$PWD/modcontact.h \
13 | $$PWD/contact.h \
14 | $$PWD/userobject.h \
15 | $$PWD/statusnotify.h \
16 | $$PWD/send_msg.h
17 |
18 | SOURCES += \
19 | $$PWD/main.cpp \
20 | $$PWD/qtsingleapplication/qtsingleapplication.cpp \
21 | $$PWD/qtsingleapplication/qtlocalpeer.cpp \
22 | $$PWD/httpget.cpp \
23 | $$PWD/uuid.cpp \
24 | $$PWD/scan.cpp \
25 | $$PWD/cookie.cpp \
26 | $$PWD/httppost.cpp \
27 | $$PWD/sec_req.cpp \
28 | $$PWD/init.cpp \
29 | $$PWD/modcontact.cpp \
30 | $$PWD/contact.cpp \
31 | $$PWD/statusnotify.cpp \
32 | $$PWD/send_msg.cpp
33 |
34 |
--------------------------------------------------------------------------------
/src/httpget.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #include "httpget.h"
4 |
5 | HttpGet::HttpGet(QObject* parent)
6 | : QObject(parent)
7 | {
8 | #if QWX_DEBUG
9 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
10 | #endif
11 | }
12 |
13 | HttpGet::~HttpGet()
14 | {
15 | #if QWX_DEBUG
16 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
17 | #endif
18 | }
19 |
20 | void HttpGet::get(QString url)
21 | {
22 | connect(&m_nam, SIGNAL(finished(QNetworkReply*)),
23 | this, SLOT(m_finished(QNetworkReply*)));
24 | connect(&m_nam, &QNetworkAccessManager::sslErrors,
25 | this, &HttpGet::m_sslErrors);
26 | m_nam.get(QNetworkRequest(url));
27 | }
28 |
29 | void HttpGet::finished(QNetworkReply*) {}
30 |
31 | void HttpGet::m_finished(QNetworkReply* reply)
32 | {
33 | this->finished(reply);
34 | disconnect(&m_nam, SIGNAL(finished(QNetworkReply*)),
35 | this, SLOT(m_finished(QNetworkReply*)));
36 | }
37 |
38 | void HttpGet::m_sslErrors(QNetworkReply* reply, const QList & errors)
39 | {
40 | reply->ignoreSslErrors(errors);
41 | #if QWX_DEBUG
42 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << reply << errors;
43 | #endif
44 | }
45 |
--------------------------------------------------------------------------------
/qwx.pro:
--------------------------------------------------------------------------------
1 | QT_VERSION = $$[QT_VERSION]
2 | QT_VERSION = $$split(QT_VERSION, ".")
3 | QT_VER_MAJ = $$member(QT_VERSION, 0)
4 | QT_VER_MIN = $$member(QT_VERSION, 1)
5 |
6 | lessThan(QT_VER_MAJ, 5) | lessThan(QT_VER_MIN, 2) | {
7 | error(qwx is only tested under Qt 5.2!)
8 | }
9 |
10 | QT += qml quick network xml
11 | QMAKE_CXXFLAGS += -std=c++11 -Werror
12 | !android: !ios: !blackberry: qtHaveModule(widgets): QT += widgets
13 | TARGET = qwx
14 | CODECFORSRC = UTF-8
15 |
16 | include(src/src.pri)
17 |
18 | OTHER_FILES += \
19 | qml/main.qml \
20 | qml/LoginView.qml \
21 | qml/ContactListView.qml
22 |
23 | RESOURCES += \
24 | resources.qrc
25 |
26 | !isEmpty(QWX_DEBUG) {
27 | DEFINES += QWX_DEBUG
28 | }
29 |
30 | unix {
31 | #VARIABLES
32 | isEmpty(PREFIX) {
33 | PREFIX = /usr
34 | }
35 | BINDIR = $$PREFIX/bin
36 | DATADIR = $$PREFIX/share
37 |
38 | DEFINES += PREFIX=\\\"$$PREFIX\\\"
39 | DEFINES += TARGET=\\\"$$TARGET\\\"
40 | DEFINES += DATADIR=\\\"$$DATADIR\\\" PKGDATADIR=\\\"$$PKGDATADIR\\\"
41 |
42 | #MAKE INSTALL
43 | INSTALLS += target desktop
44 |
45 | target.path = $$BINDIR
46 |
47 | desktop.path = $$DATADIR/applications
48 | desktop.files += $${TARGET}.desktop
49 | }
50 |
--------------------------------------------------------------------------------
/src/sec_req.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #include "sec_req.h"
4 | #include "globaldeclarations.h"
5 |
6 | SecReq::SecReq(HttpPost* parent)
7 | : HttpPost(parent)
8 | {
9 | #if QWX_DEBUG
10 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
11 | #endif
12 | }
13 |
14 | SecReq::~SecReq()
15 | {
16 | #if QWX_DEBUG
17 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
18 | #endif
19 | }
20 |
21 | void SecReq::post(QString uuid)
22 | {
23 | QString url = WX_SERVER_HOST + WX_CGI_PATH + "webwxstatreport?type=1&r=" +
24 | QString::number(time(NULL));
25 | #if QWX_DEBUG
26 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
27 | #endif
28 | QString json = "{\"BaseRequest\":{\"Uin\":0,\"Sid\":0},\"Count\":1,"
29 | "\"List\":[{\"Type\":1,\"Text\":\"" + WX_CGI_PATH + "login, "
30 | "Second Request Success, uuid: " + uuid + ", time: 190765ms\"}]}";
31 | #if QWX_DEBUG
32 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << json;
33 | #endif
34 | HttpPost::post(url, json);
35 | }
36 |
37 | void SecReq::finished(QNetworkReply* reply)
38 | {
39 | QString replyStr = QString(reply->readAll());
40 | #if QWX_DEBUG
41 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
42 | qDebug() << "DEBUG:" << replyStr;
43 | #endif
44 | }
45 |
--------------------------------------------------------------------------------
/src/init.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #include "init.h"
4 | #include "globaldeclarations.h"
5 |
6 | Init::Init(HttpPost* parent)
7 | : HttpPost(parent)
8 | {
9 | #if QWX_DEBUG
10 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
11 | #endif
12 | }
13 |
14 | Init::~Init()
15 | {
16 | #if QWX_DEBUG
17 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
18 | #endif
19 | }
20 |
21 | void Init::post(QString uin, QString sid, QString skey)
22 | {
23 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << "why webwx NOT use skey" << skey;
24 | QString url = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=" +
25 | QString::number(time(NULL));
26 | #if QWX_DEBUG
27 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
28 | #endif
29 | QString json = "{\"BaseRequest\":{\"Uin\":\"" + uin + "\",\"Sid\":\"" +
30 | sid + "\",\"Skey\":\"\",\"DeviceID\":\"" + DEVICE_ID + "\"}}";
31 | #if QWX_DEBUG
32 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << json;
33 | #endif
34 | HttpPost::post(url, json);
35 | }
36 |
37 | void Init::finished(QNetworkReply* reply)
38 | {
39 | QString replyStr = QString(reply->readAll());
40 | #if QWX_DEBUG
41 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
42 | qDebug() << "DEBUG:" << replyStr;
43 | #endif
44 | }
45 |
--------------------------------------------------------------------------------
/src/httppost.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #include "httppost.h"
4 |
5 | HttpPost::HttpPost(QObject* parent)
6 | : QObject(parent)
7 | {
8 | #if QWX_DEBUG
9 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
10 | #endif
11 | }
12 |
13 | HttpPost::~HttpPost()
14 | {
15 | #if QWX_DEBUG
16 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
17 | #endif
18 | }
19 |
20 | void HttpPost::post(QString url, QString str)
21 | {
22 | QNetworkRequest request(url);
23 | // TODO: weixin use json as HTTP POST
24 | request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
25 | connect(&m_nam, SIGNAL(finished(QNetworkReply*)),
26 | this, SLOT(m_finished(QNetworkReply*)));
27 | connect(&m_nam, &QNetworkAccessManager::sslErrors,
28 | this, &HttpPost::m_sslErrors);
29 | m_nam.post(request, str.toUtf8());
30 | }
31 |
32 | void HttpPost::finished(QNetworkReply*) {}
33 |
34 | void HttpPost::m_finished(QNetworkReply* reply)
35 | {
36 | this->finished(reply);
37 | disconnect(&m_nam, SIGNAL(finished(QNetworkReply*)),
38 | this, SLOT(m_finished(QNetworkReply*)));
39 | }
40 |
41 | void HttpPost::m_sslErrors(QNetworkReply* reply, const QList & errors)
42 | {
43 | reply->ignoreSslErrors(errors);
44 | #if QWX_DEBUG
45 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << reply << errors;
46 | #endif
47 | }
48 |
--------------------------------------------------------------------------------
/src/statusnotify.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #include "statusnotify.h"
4 | #include "globaldeclarations.h"
5 |
6 | StatusNotify::StatusNotify(HttpPost* parent)
7 | : HttpPost(parent)
8 | {
9 | #if QWX_DEBUG
10 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
11 | #endif
12 | }
13 |
14 | StatusNotify::~StatusNotify()
15 | {
16 | #if QWX_DEBUG
17 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
18 | #endif
19 | }
20 |
21 | void StatusNotify::post(QString uin, QString sid, QString skey, QString userName)
22 | {
23 | QString ts = QString::number(time(NULL));
24 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << "why webwx NOT use skey" << skey;
25 | QString url = WX_SERVER_HOST + WX_CGI_PATH + "webwxstatusnotify?r=" + ts;
26 | #if QWX_DEBUG
27 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
28 | #endif
29 | QString json = "{\"BaseRequest\":{\"Uin\":" + uin + ",\"Sid\":\"" + sid
30 | + "\",\"Skey\":\"\",\"DeviceID\":\"" + DEVICE_ID + "\"},\"Code\":3,"
31 | "\"FromUserName\":\"" + userName + "\",\"ToUserName\":\"" + userName
32 | + "\",\"ClientMsgId\":\"" + ts + "\"}";
33 | #if QWX_DEBUG
34 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << json;
35 | #endif
36 | HttpPost::post(url, json);
37 | }
38 |
39 | void StatusNotify::finished(QNetworkReply* reply)
40 | {
41 | QString replyStr = QString(reply->readAll());
42 | #if QWX_DEBUG
43 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
44 | qDebug() << "DEBUG:" << replyStr;
45 | #endif
46 | }
47 |
--------------------------------------------------------------------------------
/src/userobject.h:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #ifndef USER_OBJECT_H
4 | #define USER_OBJECT_H
5 |
6 | #include
7 | #include
8 |
9 | class UserObject : public QObject
10 | {
11 | Q_OBJECT
12 |
13 | Q_PROPERTY(QString UserName READ UserName WRITE setUserName NOTIFY UserNameChanged)
14 | Q_PROPERTY(QString NickName READ NickName WRITE setNickName NOTIFY NickNameChanged)
15 | Q_PROPERTY(QString HeadImgUrl READ HeadImgUrl WRITE setHeadImgUrl NOTIFY HeadImgUrlChanged)
16 |
17 | public:
18 | UserObject(const QString UserName,
19 | const QString NickName,
20 | const QString HeadImgUrl,
21 | QObject* parent = nullptr)
22 | : QObject(parent)
23 | {
24 | m_UserName = UserName; m_NickName = NickName; m_HeadImgUrl = HeadImgUrl;
25 | }
26 |
27 | QString UserName() const { return m_UserName; }
28 | void setUserName(const QString & UserName)
29 | { m_UserName = UserName; emit UserNameChanged(); }
30 |
31 | QString NickName() const { return m_NickName; }
32 | void setNickName(const QString & NickName)
33 | { m_NickName = NickName; emit NickNameChanged(); }
34 |
35 | QString HeadImgUrl() const { return m_HeadImgUrl; }
36 | void setHeadImgUrl(const QString & HeadImgUrl)
37 | { m_HeadImgUrl = HeadImgUrl; emit HeadImgUrlChanged(); }
38 |
39 | Q_SIGNALS:
40 | void UserNameChanged();
41 | void NickNameChanged();
42 | void HeadImgUrlChanged();
43 |
44 | private:
45 | QString m_UserName;
46 | QString m_NickName;
47 | QString m_HeadImgUrl;
48 | };
49 |
50 | #endif // USER_OBJECT_H
51 |
--------------------------------------------------------------------------------
/src/uuid.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #include "uuid.h"
4 | #include "globaldeclarations.h"
5 |
6 | UUID::UUID(HttpGet* parent)
7 | : HttpGet(parent)
8 | {
9 | #if QWX_DEBUG
10 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
11 | #endif
12 | get();
13 | }
14 |
15 | UUID::~UUID()
16 | {
17 | #if QWX_DEBUG
18 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
19 | #endif
20 | }
21 |
22 | void UUID::get()
23 | {
24 | QString url = LOGIN_SERVER_HOST + "/jslogin?appid=wx782c26e4c19acffb"
25 | "&redirect_uri=" + WX_SERVER_HOST + WX_CGI_PATH + "webwxnewloginpage"
26 | "&fun=new&lang=zh_CN&_=" + QString::number(time(NULL));
27 | #if QWX_DEBUG
28 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
29 | #endif
30 | HttpGet::get(url);
31 | }
32 |
33 | void UUID::finished(QNetworkReply* reply)
34 | {
35 | QString replyStr(reply->readAll());
36 | QString uuidStr = "";
37 | QString qruuidStr = "window.QRLogin.uuid = \"";
38 | int index = -1;
39 |
40 | #if QWX_DEBUG
41 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
42 | qDebug() << "DEBUG:" << replyStr;
43 | #endif
44 | // TODO: window.QRLogin.code = 200; window.QRLogin.uuid = "395bb96e535e47";
45 | index = replyStr.indexOf(qruuidStr) + qruuidStr.size();
46 | if (index == -1) {
47 | qWarning() << "ERROR:" << __PRETTY_FUNCTION__ << "uuid not found!";
48 | emit error();
49 | return;
50 | }
51 | uuidStr = replyStr.mid(index, replyStr.size() - index - QString("\";").size());
52 | #if QWX_DEBUG
53 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << uuidStr;
54 | #endif
55 | emit uuidChanged(uuidStr);
56 | }
57 |
--------------------------------------------------------------------------------
/src/send_msg.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #include "send_msg.h"
4 | #include "globaldeclarations.h"
5 |
6 | SendMsg::SendMsg(HttpPost* parent)
7 | : HttpPost(parent)
8 | {
9 | #if QWX_DEBUG
10 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
11 | #endif
12 | }
13 |
14 | SendMsg::~SendMsg()
15 | {
16 | #if QWX_DEBUG
17 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
18 | #endif
19 | }
20 |
21 | void SendMsg::post(QString uin,
22 | QString sid,
23 | QString skey,
24 | QString fromUserName,
25 | QString toUserName,
26 | QString content)
27 | {
28 | QString ts = QString::number(time(NULL));
29 | QString url = WX_SERVER_HOST + WX_CGI_PATH + "webwxsendmsg?sid=" + sid
30 | + "&r=" + ts;
31 | #if QWX_DEBUG
32 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
33 | #endif
34 | QString json = "{\"BaseRequest\":{\"Uin\":" + uin + ",\"Sid\":\"" + sid
35 | + "\",\"Skey\":\"" + skey + "\",\"DeviceID\":\"" + DEVICE_ID
36 | + "\"},\"Msg\":{\"FromUserName\":\"" + fromUserName
37 | + "\",\"ToUserName\":\"" + toUserName
38 | + "\",\"Type\":1,\"Content\":\"" + content + "\",\"ClientMsgId\":" + ts
39 | + ",\"LocalID\":" + ts + "}}";
40 | #if QWX_DEBUG
41 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << json;
42 | #endif
43 | HttpPost::post(url, json);
44 | }
45 |
46 | void SendMsg::finished(QNetworkReply* reply)
47 | {
48 | QString replyStr = QString(reply->readAll());
49 | #if QWX_DEBUG
50 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
51 | qDebug() << "DEBUG:" << replyStr;
52 | #endif
53 | }
54 |
--------------------------------------------------------------------------------
/qml/ContactListView.qml:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | import QtQuick 2.2
4 | import QtQuick.Controls 1.1
5 | import cn.com.isoft.qwx 1.0
6 |
7 | Item {
8 | id: contactListView
9 | width: parent.width; height: parent.height
10 |
11 | property var modContactObj
12 | property string uin
13 | property string sid
14 | property string skey
15 |
16 | SendMsg {
17 | id: sendMsgObj
18 | }
19 |
20 | ListView {
21 | id: modContactListView
22 | model: contactListView.modContactObj.modContactList
23 | anchors.fill: parent
24 |
25 | delegate: Item {
26 | height: 30
27 |
28 | /* FIXME: ./src/modcontact.cpp it ONLY works in web browser ...
29 | Image {
30 | source: modelData.HeadImgUrl
31 | }
32 | */
33 |
34 | Text {
35 | text: modelData.NickName
36 |
37 | MouseArea {
38 | anchors.fill: parent
39 | onClicked: {
40 | console.log("DEBUG: only for test sending msg")
41 | // TODO: it needs to get current login user account ;)
42 | sendMsgObj.post(contactListView.uin,
43 | contactListView.sid,
44 | contactListView.skey,
45 | "sirtoozee", // TODO: only for test
46 | modelData.UserName,
47 | "hello, " + modelData.NickName + " 消息来自qwx ;)")
48 | }
49 | }
50 | }
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #include
4 | #include
5 |
6 | #include "globaldeclarations.h"
7 | #include "qtsingleapplication/QtSingleApplication"
8 | #include "uuid.h"
9 | #include "scan.h"
10 | #include "cookie.h"
11 | #include "sec_req.h"
12 | #include "init.h"
13 | #include "modcontact.h"
14 | #include "contact.h"
15 | #include "statusnotify.h"
16 | #include "send_msg.h"
17 |
18 | int main(int argc, char* argv[])
19 | {
20 | QtSingleApplication app(argc, argv);
21 | if (app.isRunning()) return 0;
22 | app.setApplicationName(CODE_NAME);
23 | app.setApplicationVersion(APPLICATION_VERSION);
24 |
25 | qmlRegisterType("cn.com.isoft.qwx", 1, 0, "UUID");
26 | qmlRegisterType("cn.com.isoft.qwx", 1, 0, "Scan");
27 | qmlRegisterType("cn.com.isoft.qwx", 1, 0, "Cookie");
28 | qmlRegisterType("cn.com.isoft.qwx", 1, 0, "SecReq");
29 | qmlRegisterType("cn.com.isoft.qwx", 1, 0, "Init");
30 | qmlRegisterType("cn.com.isoft.qwx", 1, 0, "ModContact");
31 | qmlRegisterType("cn.com.isoft.qwx", 1, 0, "Contact");
32 | qmlRegisterType("cn.com.isoft.qwx", 1, 0, "StatusNotify");
33 | qmlRegisterType("cn.com.isoft.qwx", 1, 0, "SendMsg");
34 |
35 | QQmlApplicationEngine engine(QUrl("qrc:/qml/main.qml"));
36 | QObject* topLevel = engine.rootObjects().value(0);
37 | QQuickWindow* window = qobject_cast(topLevel);
38 | if (!window) {
39 | qWarning("Error: Your root item has to be a Window.");
40 | return -1;
41 | }
42 | window->show();
43 |
44 | return app.exec();
45 | }
46 |
--------------------------------------------------------------------------------
/src/cookie.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #include
4 | #include
5 |
6 | #include "cookie.h"
7 |
8 | Cookie::Cookie(HttpGet* parent)
9 | : HttpGet(parent)
10 | {
11 | #if QWX_DEBUG
12 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
13 | #endif
14 | }
15 |
16 | Cookie::~Cookie()
17 | {
18 | #if QWX_DEBUG
19 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
20 | #endif
21 | }
22 |
23 | void Cookie::get(QString redirect_uri)
24 | {
25 | QString url = redirect_uri + "&fun=new";
26 | #if QWX_DEBUG
27 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
28 | #endif
29 | HttpGet::get(url);
30 | }
31 |
32 | void Cookie::finished(QNetworkReply* reply)
33 | {
34 | QString replyStr(reply->readAll());
35 | QDomDocument doc;
36 |
37 | #if QWX_DEBUG
38 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
39 | qDebug() << "DEBUG:" << replyStr;
40 | #endif
41 | // FIXME: my mother`s (new registed) accout is not able to get cookie!?
42 | // but my wife and mine are OK, weixin changed web API?
43 | if (doc.setContent(replyStr) == false) {
44 | qWarning() << "ERROR:" << __PRETTY_FUNCTION__ << "fail to parse";
45 | return;
46 | }
47 | QDomElement root = doc.documentElement();
48 | QDomElement message = root.firstChildElement("message");
49 | QDomElement skey = root.firstChildElement("skey");
50 | QDomElement sid = root.firstChildElement("wxsid");
51 | QDomElement uin = root.firstChildElement("wxuin");
52 | QDomElement ticket = root.firstChildElement("pass_ticket");
53 | #if QWX_DEBUG
54 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << message.text() <<
55 | skey.text() << sid.text() << uin.text() << ticket.text();
56 | #endif
57 | emit infoChanged(skey.text(), sid.text(), uin.text(), ticket.text());
58 | }
59 |
60 |
--------------------------------------------------------------------------------
/src/scan.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #include "scan.h"
4 | #include "globaldeclarations.h"
5 |
6 | Scan::Scan(HttpGet* parent)
7 | : HttpGet(parent)
8 | {
9 | #if QWX_DEBUG
10 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
11 | #endif
12 | }
13 |
14 | Scan::~Scan()
15 | {
16 | #if QWX_DEBUG
17 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
18 | #endif
19 | }
20 |
21 | void Scan::get(QString uuid)
22 | {
23 | QString url = LOGIN_SERVER_HOST + WX_CGI_PATH + "login?uuid=" + uuid +
24 | "&tip=0&_=" + QString::number(time(NULL));
25 | #if QWX_DEBUG
26 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
27 | #endif
28 | HttpGet::get(url);
29 | }
30 |
31 | void Scan::finished(QNetworkReply* reply)
32 | {
33 | QString replyStr(reply->readAll());
34 | QString qrredirect_uriStr = "window.redirect_uri=\"";
35 | QString redirect_uriStr = "";
36 | int index = -1;
37 |
38 | #if QWX_DEBUG
39 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
40 | qDebug() << "DEBUG:" << replyStr;
41 | #endif
42 | if (replyStr == "window.code=408;") {
43 | emit error("timeout");
44 | return;
45 | }
46 | if (replyStr == "window.code=201;") {
47 | emit scanedButWaitConfirm();
48 | return;
49 | }
50 | if (replyStr.contains("window.code=200;")) {
51 | index = replyStr.indexOf(qrredirect_uriStr) + qrredirect_uriStr.size();
52 | if (index == -1) {
53 | qWarning() << "ERROR:" << __PRETTY_FUNCTION__ <<
54 | "redirect_uri not found!";
55 | emit scanedAndConfirmed(redirect_uriStr);
56 | return;
57 | }
58 | redirect_uriStr = replyStr.mid(index, replyStr.size() - index - 2);
59 | #if QWX_DEBUG
60 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << redirect_uriStr;
61 | #endif
62 | emit scanedAndConfirmed(redirect_uriStr);
63 | return;
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/src/modcontact.cpp:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | #if QWX_DEBUG
4 | #include
5 | #endif
6 | #include
7 | #include
8 | #include
9 |
10 | #include "modcontact.h"
11 | #include "userobject.h"
12 | #include "globaldeclarations.h"
13 |
14 | ModContact::ModContact(HttpPost* parent)
15 | : HttpPost(parent)
16 | {
17 | #if QWX_DEBUG
18 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
19 | #endif
20 | }
21 |
22 | ModContact::~ModContact()
23 | {
24 | #if QWX_DEBUG
25 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__;
26 | #endif
27 | }
28 |
29 | void ModContact::post(QString uin, QString sid)
30 | {
31 | QString ts = QString::number(time(NULL));
32 | QString url = WX_SERVER_HOST + WX_CGI_PATH + "webwxsync?sid=" + sid + "&r=" + ts;
33 | #if QWX_DEBUG
34 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << url;
35 | #endif
36 | QString json = "{\"BaseRequest\":{\"Uin\":" + uin + ",\"Sid\":\"" + sid +
37 | "\"},\"SyncKey\":{\"Count\":4,\"List\":[{\"Key\":1,\"Val\":620916854},"
38 | "{\"Key\":2,\"Val\":620917961},{\"Key\":3,\"Val\":620917948},"
39 | "{\"Key\":1000,\"Val\":1388967977}]},\"rr\":" + ts + "}";
40 | #if QWX_DEBUG
41 | qDebug() << "DEBUG:" << __PRETTY_FUNCTION__ << json;
42 | #endif
43 | HttpPost::post(url, json);
44 | }
45 |
46 | QList ModContact::modContactList() const { return m_modContactList; }
47 |
48 | void ModContact::finished(QNetworkReply* reply)
49 | {
50 | QString replyStr = QString(reply->readAll());
51 | #if QWX_DEBUG
52 | QFile file("modcontact.json");
53 | if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
54 | QTextStream out(&file);
55 | out << replyStr;
56 | }
57 | #endif
58 | QJsonDocument doc = QJsonDocument::fromJson(replyStr.toUtf8());
59 | if (!doc.isObject()) { emit error(); return; }
60 | QJsonObject obj = doc.object();
61 | QJsonArray arr = obj["ModContactList"].toArray();
62 | foreach (const QJsonValue & val, arr) {
63 | QJsonObject user = val.toObject();
64 | m_modContactList.append(new UserObject(
65 | user["UserName"].toString(),
66 | user["NickName"].toString(),
67 | WX_SERVER_HOST + user["HeadImgUrl"].toString()));
68 | }
69 | emit modContactListChanged();
70 | }
71 |
--------------------------------------------------------------------------------
/qml/LoginView.qml:
--------------------------------------------------------------------------------
1 | // Copyright (C) 2014 Leslie Zhai
2 |
3 | import QtQuick 2.2
4 | import QtQuick.Controls 1.1
5 | import cn.com.isoft.qwx 1.0
6 |
7 | Item {
8 | id: loginView
9 | width: parent.width; height: parent.height
10 |
11 | property string uuid: ""
12 |
13 | UUID {
14 | id: uuidObj
15 | onError: {
16 | console.log("ERROR: fail to get uuid!")
17 | }
18 | onUuidChanged: {
19 | loginView.uuid = uuid
20 | qrcodeImage.source = "https://login.weixin.qq.com/qrcode/" + uuid + "?t=webwx"
21 | qrcodeImage.visible = true
22 | scanQRcode()
23 | scanTimer.start()
24 | }
25 | }
26 |
27 | Image {
28 | id: qrcodeImage
29 | visible: false
30 | }
31 |
32 | Scan {
33 | id: scanObj
34 | onError: {
35 | console.log("ERROR:", strerror)
36 | }
37 | onScanedButWaitConfirm: {
38 | console.log("DEBUG: scaned but waitting for confirm ...")
39 | }
40 | onScanedAndConfirmed: {
41 | console.log("DEBUG: confirmed")
42 | scanTimer.stop()
43 | cookieObj.get(redirect_uri)
44 | secReqObj.post(loginView.uuid)
45 | }
46 | }
47 |
48 | function scanQRcode() { scanObj.get(loginView.uuid) }
49 |
50 | Timer {
51 | id: scanTimer
52 | interval: 16000; running: false; repeat: true
53 | onTriggered: { scanQRcode() }
54 | }
55 |
56 | Cookie {
57 | id: cookieObj
58 | onInfoChanged: {
59 | initObj.post(uin, sid, skey)
60 | modContactObj.post(uin, sid)
61 | contactObj.post()
62 | //-----------------------------------------------------------------
63 | // TODO: only for test
64 | statusNotifyObj.post(uin, sid, skey, "sirtoozee")
65 | //-----------------------------------------------------------------
66 | stackView.clear()
67 | stackView.push({item: Qt.resolvedUrl("ContactListView.qml"),
68 | properties: {modContactObj: modContactObj,
69 | uin: uin, sid: sid, skey: skey}})
70 | }
71 | }
72 |
73 | SecReq {
74 | id: secReqObj
75 | }
76 |
77 | Init {
78 | id: initObj
79 | }
80 |
81 | ModContact {
82 | id: modContactObj
83 | }
84 |
85 | Contact {
86 | id: contactObj
87 | }
88 |
89 | StatusNotify {
90 | id: statusNotifyObj
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/qtsingleapplication/qtsinglecoreapplication.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | **
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 | ** Contact: http://www.qt-project.org/legal
5 | **
6 | ** This file is part of the Qt Solutions component.
7 | **
8 | ** $QT_BEGIN_LICENSE:BSD$
9 | ** You may use this file under the terms of the BSD license as follows:
10 | **
11 | ** "Redistribution and use in source and binary forms, with or without
12 | ** modification, are permitted provided that the following conditions are
13 | ** met:
14 | ** * Redistributions of source code must retain the above copyright
15 | ** notice, this list of conditions and the following disclaimer.
16 | ** * Redistributions in binary form must reproduce the above copyright
17 | ** notice, this list of conditions and the following disclaimer in
18 | ** the documentation and/or other materials provided with the
19 | ** distribution.
20 | ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 | ** of its contributors may be used to endorse or promote products derived
22 | ** from this software without specific prior written permission.
23 | **
24 | **
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 | **
37 | ** $QT_END_LICENSE$
38 | **
39 | ****************************************************************************/
40 |
41 | #ifndef QTSINGLECOREAPPLICATION_H
42 | #define QTSINGLECOREAPPLICATION_H
43 |
44 | #include
45 |
46 | class QtLocalPeer;
47 |
48 | class QtSingleCoreApplication : public QCoreApplication
49 | {
50 | Q_OBJECT
51 |
52 | public:
53 | QtSingleCoreApplication(int &argc, char **argv);
54 | QtSingleCoreApplication(const QString &id, int &argc, char **argv);
55 |
56 | bool isRunning();
57 | QString id() const;
58 |
59 | public Q_SLOTS:
60 | bool sendMessage(const QString &message, int timeout = 5000);
61 |
62 |
63 | Q_SIGNALS:
64 | void messageReceived(const QString &message);
65 |
66 |
67 | private:
68 | QtLocalPeer* peer;
69 | };
70 |
71 | #endif // QTSINGLECOREAPPLICATION_H
72 |
--------------------------------------------------------------------------------
/src/qtsingleapplication/qtlocalpeer.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | **
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 | ** Contact: http://www.qt-project.org/legal
5 | **
6 | ** This file is part of the Qt Solutions component.
7 | **
8 | ** $QT_BEGIN_LICENSE:BSD$
9 | ** You may use this file under the terms of the BSD license as follows:
10 | **
11 | ** "Redistribution and use in source and binary forms, with or without
12 | ** modification, are permitted provided that the following conditions are
13 | ** met:
14 | ** * Redistributions of source code must retain the above copyright
15 | ** notice, this list of conditions and the following disclaimer.
16 | ** * Redistributions in binary form must reproduce the above copyright
17 | ** notice, this list of conditions and the following disclaimer in
18 | ** the documentation and/or other materials provided with the
19 | ** distribution.
20 | ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 | ** of its contributors may be used to endorse or promote products derived
22 | ** from this software without specific prior written permission.
23 | **
24 | **
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 | **
37 | ** $QT_END_LICENSE$
38 | **
39 | ****************************************************************************/
40 |
41 | #ifndef QTLOCALPEER_H
42 | #define QTLOCALPEER_H
43 |
44 | #include
45 | #include
46 | #include
47 |
48 | #include "qtlockedfile.h"
49 |
50 | class QtLocalPeer : public QObject
51 | {
52 | Q_OBJECT
53 |
54 | public:
55 | QtLocalPeer(QObject *parent = 0, const QString &appId = QString());
56 | bool isClient();
57 | bool sendMessage(const QString &message, int timeout);
58 | QString applicationId() const
59 | { return id; }
60 |
61 | Q_SIGNALS:
62 | void messageReceived(const QString &message);
63 |
64 | protected Q_SLOTS:
65 | void receiveConnection();
66 |
67 | protected:
68 | QString id;
69 | QString socketName;
70 | QLocalServer* server;
71 | QtLP_Private::QtLockedFile lockFile;
72 |
73 | private:
74 | static const char* ack;
75 | };
76 |
77 | #endif // QTLOCALPEER_H
78 |
--------------------------------------------------------------------------------
/src/qtsingleapplication/qtlockedfile.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | **
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 | ** Contact: http://www.qt-project.org/legal
5 | **
6 | ** This file is part of the Qt Solutions component.
7 | **
8 | ** $QT_BEGIN_LICENSE:BSD$
9 | ** You may use this file under the terms of the BSD license as follows:
10 | **
11 | ** "Redistribution and use in source and binary forms, with or without
12 | ** modification, are permitted provided that the following conditions are
13 | ** met:
14 | ** * Redistributions of source code must retain the above copyright
15 | ** notice, this list of conditions and the following disclaimer.
16 | ** * Redistributions in binary form must reproduce the above copyright
17 | ** notice, this list of conditions and the following disclaimer in
18 | ** the documentation and/or other materials provided with the
19 | ** distribution.
20 | ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 | ** of its contributors may be used to endorse or promote products derived
22 | ** from this software without specific prior written permission.
23 | **
24 | **
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 | **
37 | ** $QT_END_LICENSE$
38 | **
39 | ****************************************************************************/
40 |
41 | #ifndef QTLOCKEDFILE_H
42 | #define QTLOCKEDFILE_H
43 |
44 | #include
45 | #ifdef Q_OS_WIN
46 | #include
47 | #endif
48 |
49 | #if defined(Q_OS_WIN)
50 | # if !defined(QT_QTLOCKEDFILE_EXPORT) && !defined(QT_QTLOCKEDFILE_IMPORT)
51 | # define QT_QTLOCKEDFILE_EXPORT
52 | # elif defined(QT_QTLOCKEDFILE_IMPORT)
53 | # if defined(QT_QTLOCKEDFILE_EXPORT)
54 | # undef QT_QTLOCKEDFILE_EXPORT
55 | # endif
56 | # define QT_QTLOCKEDFILE_EXPORT __declspec(dllimport)
57 | # elif defined(QT_QTLOCKEDFILE_EXPORT)
58 | # undef QT_QTLOCKEDFILE_EXPORT
59 | # define QT_QTLOCKEDFILE_EXPORT __declspec(dllexport)
60 | # endif
61 | #else
62 | # define QT_QTLOCKEDFILE_EXPORT
63 | #endif
64 |
65 | namespace QtLP_Private {
66 |
67 | class QT_QTLOCKEDFILE_EXPORT QtLockedFile : public QFile
68 | {
69 | public:
70 | enum LockMode { NoLock = 0, ReadLock, WriteLock };
71 |
72 | QtLockedFile();
73 | QtLockedFile(const QString &name);
74 | ~QtLockedFile();
75 |
76 | bool open(OpenMode mode);
77 |
78 | bool lock(LockMode mode, bool block = true);
79 | bool unlock();
80 | bool isLocked() const;
81 | LockMode lockMode() const;
82 |
83 | private:
84 | #ifdef Q_OS_WIN
85 | Qt::HANDLE wmutex;
86 | Qt::HANDLE rmutex;
87 | QVector rmutexes;
88 | QString mutexname;
89 |
90 | Qt::HANDLE getMutexHandle(int idx, bool doCreate);
91 | bool waitMutex(Qt::HANDLE mutex, bool doBlock);
92 |
93 | #endif
94 | LockMode m_lock_mode;
95 | };
96 | }
97 | #endif
98 |
--------------------------------------------------------------------------------
/src/qtsingleapplication/qtlockedfile_unix.cpp:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | **
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 | ** Contact: http://www.qt-project.org/legal
5 | **
6 | ** This file is part of the Qt Solutions component.
7 | **
8 | ** $QT_BEGIN_LICENSE:BSD$
9 | ** You may use this file under the terms of the BSD license as follows:
10 | **
11 | ** "Redistribution and use in source and binary forms, with or without
12 | ** modification, are permitted provided that the following conditions are
13 | ** met:
14 | ** * Redistributions of source code must retain the above copyright
15 | ** notice, this list of conditions and the following disclaimer.
16 | ** * Redistributions in binary form must reproduce the above copyright
17 | ** notice, this list of conditions and the following disclaimer in
18 | ** the documentation and/or other materials provided with the
19 | ** distribution.
20 | ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 | ** of its contributors may be used to endorse or promote products derived
22 | ** from this software without specific prior written permission.
23 | **
24 | **
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 | **
37 | ** $QT_END_LICENSE$
38 | **
39 | ****************************************************************************/
40 |
41 | #include
42 | #include
43 | #include
44 | #include
45 |
46 | #include "qtlockedfile.h"
47 |
48 | bool QtLockedFile::lock(LockMode mode, bool block)
49 | {
50 | if (!isOpen()) {
51 | qWarning("QtLockedFile::lock(): file is not opened");
52 | return false;
53 | }
54 |
55 | if (mode == NoLock)
56 | return unlock();
57 |
58 | if (mode == m_lock_mode)
59 | return true;
60 |
61 | if (m_lock_mode != NoLock)
62 | unlock();
63 |
64 | struct flock fl;
65 | fl.l_whence = SEEK_SET;
66 | fl.l_start = 0;
67 | fl.l_len = 0;
68 | fl.l_type = (mode == ReadLock) ? F_RDLCK : F_WRLCK;
69 | int cmd = block ? F_SETLKW : F_SETLK;
70 | int ret = fcntl(handle(), cmd, &fl);
71 |
72 | if (ret == -1) {
73 | if (errno != EINTR && errno != EAGAIN)
74 | qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
75 | return false;
76 | }
77 |
78 |
79 | m_lock_mode = mode;
80 | return true;
81 | }
82 |
83 |
84 | bool QtLockedFile::unlock()
85 | {
86 | if (!isOpen()) {
87 | qWarning("QtLockedFile::unlock(): file is not opened");
88 | return false;
89 | }
90 |
91 | if (!isLocked())
92 | return true;
93 |
94 | struct flock fl;
95 | fl.l_whence = SEEK_SET;
96 | fl.l_start = 0;
97 | fl.l_len = 0;
98 | fl.l_type = F_UNLCK;
99 | int ret = fcntl(handle(), F_SETLKW, &fl);
100 |
101 | if (ret == -1) {
102 | qWarning("QtLockedFile::lock(): fcntl: %s", strerror(errno));
103 | return false;
104 | }
105 |
106 | m_lock_mode = NoLock;
107 | return true;
108 | }
109 |
110 | QtLockedFile::~QtLockedFile()
111 | {
112 | if (isOpen())
113 | unlock();
114 | }
115 |
116 |
--------------------------------------------------------------------------------
/src/qtsingleapplication/qtsingleapplication.h:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | **
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 | ** Contact: http://www.qt-project.org/legal
5 | **
6 | ** This file is part of the Qt Solutions component.
7 | **
8 | ** $QT_BEGIN_LICENSE:BSD$
9 | ** You may use this file under the terms of the BSD license as follows:
10 | **
11 | ** "Redistribution and use in source and binary forms, with or without
12 | ** modification, are permitted provided that the following conditions are
13 | ** met:
14 | ** * Redistributions of source code must retain the above copyright
15 | ** notice, this list of conditions and the following disclaimer.
16 | ** * Redistributions in binary form must reproduce the above copyright
17 | ** notice, this list of conditions and the following disclaimer in
18 | ** the documentation and/or other materials provided with the
19 | ** distribution.
20 | ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 | ** of its contributors may be used to endorse or promote products derived
22 | ** from this software without specific prior written permission.
23 | **
24 | **
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 | **
37 | ** $QT_END_LICENSE$
38 | **
39 | ****************************************************************************/
40 |
41 | #ifndef QTSINGLEAPPLICATION_H
42 | #define QTSINGLEAPPLICATION_H
43 |
44 | #include
45 |
46 | class QtLocalPeer;
47 |
48 | #if defined(Q_OS_WIN)
49 | # if !defined(QT_QTSINGLEAPPLICATION_EXPORT) && !defined(QT_QTSINGLEAPPLICATION_IMPORT)
50 | # define QT_QTSINGLEAPPLICATION_EXPORT
51 | # elif defined(QT_QTSINGLEAPPLICATION_IMPORT)
52 | # if defined(QT_QTSINGLEAPPLICATION_EXPORT)
53 | # undef QT_QTSINGLEAPPLICATION_EXPORT
54 | # endif
55 | # define QT_QTSINGLEAPPLICATION_EXPORT __declspec(dllimport)
56 | # elif defined(QT_QTSINGLEAPPLICATION_EXPORT)
57 | # undef QT_QTSINGLEAPPLICATION_EXPORT
58 | # define QT_QTSINGLEAPPLICATION_EXPORT __declspec(dllexport)
59 | # endif
60 | #else
61 | # define QT_QTSINGLEAPPLICATION_EXPORT
62 | #endif
63 |
64 | class QT_QTSINGLEAPPLICATION_EXPORT QtSingleApplication : public QApplication
65 | {
66 | Q_OBJECT
67 |
68 | public:
69 | QtSingleApplication(int &argc, char **argv, bool GUIenabled = true);
70 | QtSingleApplication(const QString &id, int &argc, char **argv);
71 | #if QT_VERSION < 0x050000
72 | QtSingleApplication(int &argc, char **argv, Type type);
73 | # if defined(Q_WS_X11)
74 | QtSingleApplication(Display* dpy, Qt::HANDLE visual = 0, Qt::HANDLE colormap = 0);
75 | QtSingleApplication(Display *dpy, int &argc, char **argv, Qt::HANDLE visual = 0, Qt::HANDLE cmap= 0);
76 | QtSingleApplication(Display* dpy, const QString &appId, int argc, char **argv, Qt::HANDLE visual = 0, Qt::HANDLE colormap = 0);
77 | # endif // Q_WS_X11
78 | #endif // QT_VERSION < 0x050000
79 |
80 | bool isRunning();
81 | QString id() const;
82 |
83 | void setActivationWindow(QWidget* aw, bool activateOnMessage = true);
84 | QWidget* activationWindow() const;
85 |
86 | // Obsolete:
87 | void initialize(bool dummy = true)
88 | { isRunning(); Q_UNUSED(dummy) }
89 |
90 | public Q_SLOTS:
91 | bool sendMessage(const QString &message, int timeout = 5000);
92 | void activateWindow();
93 |
94 |
95 | Q_SIGNALS:
96 | void messageReceived(const QString &message);
97 |
98 |
99 | private:
100 | void sysInit(const QString &appId = QString());
101 | QtLocalPeer *peer;
102 | QWidget *actWin;
103 | };
104 |
105 | #endif // QTSINGLEAPPLICATION_H
106 |
--------------------------------------------------------------------------------
/src/qtsingleapplication/qtsinglecoreapplication.cpp:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | **
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 | ** Contact: http://www.qt-project.org/legal
5 | **
6 | ** This file is part of the Qt Solutions component.
7 | **
8 | ** $QT_BEGIN_LICENSE:BSD$
9 | ** You may use this file under the terms of the BSD license as follows:
10 | **
11 | ** "Redistribution and use in source and binary forms, with or without
12 | ** modification, are permitted provided that the following conditions are
13 | ** met:
14 | ** * Redistributions of source code must retain the above copyright
15 | ** notice, this list of conditions and the following disclaimer.
16 | ** * Redistributions in binary form must reproduce the above copyright
17 | ** notice, this list of conditions and the following disclaimer in
18 | ** the documentation and/or other materials provided with the
19 | ** distribution.
20 | ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 | ** of its contributors may be used to endorse or promote products derived
22 | ** from this software without specific prior written permission.
23 | **
24 | **
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 | **
37 | ** $QT_END_LICENSE$
38 | **
39 | ****************************************************************************/
40 |
41 |
42 | #include "qtsinglecoreapplication.h"
43 | #include "qtlocalpeer.h"
44 |
45 | /*!
46 | \class QtSingleCoreApplication qtsinglecoreapplication.h
47 | \brief A variant of the QtSingleApplication class for non-GUI applications.
48 |
49 | This class is a variant of QtSingleApplication suited for use in
50 | console (non-GUI) applications. It is an extension of
51 | QCoreApplication (instead of QApplication). It does not require
52 | the QtGui library.
53 |
54 | The API and usage is identical to QtSingleApplication, except that
55 | functions relating to the "activation window" are not present, for
56 | obvious reasons. Please refer to the QtSingleApplication
57 | documentation for explanation of the usage.
58 |
59 | A QtSingleCoreApplication instance can communicate to a
60 | QtSingleApplication instance if they share the same application
61 | id. Hence, this class can be used to create a light-weight
62 | command-line tool that sends commands to a GUI application.
63 |
64 | \sa QtSingleApplication
65 | */
66 |
67 | /*!
68 | Creates a QtSingleCoreApplication object. The application identifier
69 | will be QCoreApplication::applicationFilePath(). \a argc and \a
70 | argv are passed on to the QCoreAppliation constructor.
71 | */
72 |
73 | QtSingleCoreApplication::QtSingleCoreApplication(int &argc, char **argv)
74 | : QCoreApplication(argc, argv)
75 | {
76 | peer = new QtLocalPeer(this);
77 | connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&)));
78 | }
79 |
80 |
81 | /*!
82 | Creates a QtSingleCoreApplication object with the application
83 | identifier \a appId. \a argc and \a argv are passed on to the
84 | QCoreAppliation constructor.
85 | */
86 | QtSingleCoreApplication::QtSingleCoreApplication(const QString &appId, int &argc, char **argv)
87 | : QCoreApplication(argc, argv)
88 | {
89 | peer = new QtLocalPeer(this, appId);
90 | connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&)));
91 | }
92 |
93 |
94 | /*!
95 | Returns true if another instance of this application is running;
96 | otherwise false.
97 |
98 | This function does not find instances of this application that are
99 | being run by a different user (on Windows: that are running in
100 | another session).
101 |
102 | \sa sendMessage()
103 | */
104 |
105 | bool QtSingleCoreApplication::isRunning()
106 | {
107 | return peer->isClient();
108 | }
109 |
110 |
111 | /*!
112 | Tries to send the text \a message to the currently running
113 | instance. The QtSingleCoreApplication object in the running instance
114 | will emit the messageReceived() signal when it receives the
115 | message.
116 |
117 | This function returns true if the message has been sent to, and
118 | processed by, the current instance. If there is no instance
119 | currently running, or if the running instance fails to process the
120 | message within \a timeout milliseconds, this function return false.
121 |
122 | \sa isRunning(), messageReceived()
123 | */
124 |
125 | bool QtSingleCoreApplication::sendMessage(const QString &message, int timeout)
126 | {
127 | return peer->sendMessage(message, timeout);
128 | }
129 |
130 |
131 | /*!
132 | Returns the application identifier. Two processes with the same
133 | identifier will be regarded as instances of the same application.
134 | */
135 |
136 | QString QtSingleCoreApplication::id() const
137 | {
138 | return peer->applicationId();
139 | }
140 |
141 |
142 | /*!
143 | \fn void QtSingleCoreApplication::messageReceived(const QString& message)
144 |
145 | This signal is emitted when the current instance receives a \a
146 | message from another instance of this application.
147 |
148 | \sa sendMessage()
149 | */
150 |
--------------------------------------------------------------------------------
/src/qtsingleapplication/qtlockedfile.cpp:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | **
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 | ** Contact: http://www.qt-project.org/legal
5 | **
6 | ** This file is part of the Qt Solutions component.
7 | **
8 | ** $QT_BEGIN_LICENSE:BSD$
9 | ** You may use this file under the terms of the BSD license as follows:
10 | **
11 | ** "Redistribution and use in source and binary forms, with or without
12 | ** modification, are permitted provided that the following conditions are
13 | ** met:
14 | ** * Redistributions of source code must retain the above copyright
15 | ** notice, this list of conditions and the following disclaimer.
16 | ** * Redistributions in binary form must reproduce the above copyright
17 | ** notice, this list of conditions and the following disclaimer in
18 | ** the documentation and/or other materials provided with the
19 | ** distribution.
20 | ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 | ** of its contributors may be used to endorse or promote products derived
22 | ** from this software without specific prior written permission.
23 | **
24 | **
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 | **
37 | ** $QT_END_LICENSE$
38 | **
39 | ****************************************************************************/
40 |
41 | #include "qtlockedfile.h"
42 |
43 | /*!
44 | \class QtLockedFile
45 |
46 | \brief The QtLockedFile class extends QFile with advisory locking
47 | functions.
48 |
49 | A file may be locked in read or write mode. Multiple instances of
50 | \e QtLockedFile, created in multiple processes running on the same
51 | machine, may have a file locked in read mode. Exactly one instance
52 | may have it locked in write mode. A read and a write lock cannot
53 | exist simultaneously on the same file.
54 |
55 | The file locks are advisory. This means that nothing prevents
56 | another process from manipulating a locked file using QFile or
57 | file system functions offered by the OS. Serialization is only
58 | guaranteed if all processes that access the file use
59 | QLockedFile. Also, while holding a lock on a file, a process
60 | must not open the same file again (through any API), or locks
61 | can be unexpectedly lost.
62 |
63 | The lock provided by an instance of \e QtLockedFile is released
64 | whenever the program terminates. This is true even when the
65 | program crashes and no destructors are called.
66 | */
67 |
68 | /*! \enum QtLockedFile::LockMode
69 |
70 | This enum describes the available lock modes.
71 |
72 | \value ReadLock A read lock.
73 | \value WriteLock A write lock.
74 | \value NoLock Neither a read lock nor a write lock.
75 | */
76 |
77 | /*!
78 | Constructs an unlocked \e QtLockedFile object. This constructor
79 | behaves in the same way as \e QFile::QFile().
80 |
81 | \sa QFile::QFile()
82 | */
83 | QtLockedFile::QtLockedFile()
84 | : QFile()
85 | {
86 | #ifdef Q_OS_WIN
87 | wmutex = 0;
88 | rmutex = 0;
89 | #endif
90 | m_lock_mode = NoLock;
91 | }
92 |
93 | /*!
94 | Constructs an unlocked QtLockedFile object with file \a name. This
95 | constructor behaves in the same way as \e QFile::QFile(const
96 | QString&).
97 |
98 | \sa QFile::QFile()
99 | */
100 | QtLockedFile::QtLockedFile(const QString &name)
101 | : QFile(name)
102 | {
103 | #ifdef Q_OS_WIN
104 | wmutex = 0;
105 | rmutex = 0;
106 | #endif
107 | m_lock_mode = NoLock;
108 | }
109 |
110 | /*!
111 | Opens the file in OpenMode \a mode.
112 |
113 | This is identical to QFile::open(), with the one exception that the
114 | Truncate mode flag is disallowed. Truncation would conflict with the
115 | advisory file locking, since the file would be modified before the
116 | write lock is obtained. If truncation is required, use resize(0)
117 | after obtaining the write lock.
118 |
119 | Returns true if successful; otherwise false.
120 |
121 | \sa QFile::open(), QFile::resize()
122 | */
123 | bool QtLockedFile::open(OpenMode mode)
124 | {
125 | if (mode & QIODevice::Truncate) {
126 | qWarning("QtLockedFile::open(): Truncate mode not allowed.");
127 | return false;
128 | }
129 | return QFile::open(mode);
130 | }
131 |
132 | /*!
133 | Returns \e true if this object has a in read or write lock;
134 | otherwise returns \e false.
135 |
136 | \sa lockMode()
137 | */
138 | bool QtLockedFile::isLocked() const
139 | {
140 | return m_lock_mode != NoLock;
141 | }
142 |
143 | /*!
144 | Returns the type of lock currently held by this object, or \e
145 | QtLockedFile::NoLock.
146 |
147 | \sa isLocked()
148 | */
149 | QtLockedFile::LockMode QtLockedFile::lockMode() const
150 | {
151 | return m_lock_mode;
152 | }
153 |
154 | /*!
155 | \fn bool QtLockedFile::lock(LockMode mode, bool block = true)
156 |
157 | Obtains a lock of type \a mode. The file must be opened before it
158 | can be locked.
159 |
160 | If \a block is true, this function will block until the lock is
161 | aquired. If \a block is false, this function returns \e false
162 | immediately if the lock cannot be aquired.
163 |
164 | If this object already has a lock of type \a mode, this function
165 | returns \e true immediately. If this object has a lock of a
166 | different type than \a mode, the lock is first released and then a
167 | new lock is obtained.
168 |
169 | This function returns \e true if, after it executes, the file is
170 | locked by this object, and \e false otherwise.
171 |
172 | \sa unlock(), isLocked(), lockMode()
173 | */
174 |
175 | /*!
176 | \fn bool QtLockedFile::unlock()
177 |
178 | Releases a lock.
179 |
180 | If the object has no lock, this function returns immediately.
181 |
182 | This function returns \e true if, after it executes, the file is
183 | not locked by this object, and \e false otherwise.
184 |
185 | \sa lock(), isLocked(), lockMode()
186 | */
187 |
188 | /*!
189 | \fn QtLockedFile::~QtLockedFile()
190 |
191 | Destroys the \e QtLockedFile object. If any locks were held, they
192 | are released.
193 | */
194 |
--------------------------------------------------------------------------------
/src/qtsingleapplication/qtlockedfile_win.cpp:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | **
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 | ** Contact: http://www.qt-project.org/legal
5 | **
6 | ** This file is part of the Qt Solutions component.
7 | **
8 | ** $QT_BEGIN_LICENSE:BSD$
9 | ** You may use this file under the terms of the BSD license as follows:
10 | **
11 | ** "Redistribution and use in source and binary forms, with or without
12 | ** modification, are permitted provided that the following conditions are
13 | ** met:
14 | ** * Redistributions of source code must retain the above copyright
15 | ** notice, this list of conditions and the following disclaimer.
16 | ** * Redistributions in binary form must reproduce the above copyright
17 | ** notice, this list of conditions and the following disclaimer in
18 | ** the documentation and/or other materials provided with the
19 | ** distribution.
20 | ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 | ** of its contributors may be used to endorse or promote products derived
22 | ** from this software without specific prior written permission.
23 | **
24 | **
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 | **
37 | ** $QT_END_LICENSE$
38 | **
39 | ****************************************************************************/
40 |
41 | #include "qtlockedfile.h"
42 | #include
43 | #include
44 |
45 | #define MUTEX_PREFIX "QtLockedFile mutex "
46 | // Maximum number of concurrent read locks. Must not be greater than MAXIMUM_WAIT_OBJECTS
47 | #define MAX_READERS MAXIMUM_WAIT_OBJECTS
48 |
49 | #if QT_VERSION >= 0x050000
50 | #define QT_WA(unicode, ansi) unicode
51 | #endif
52 |
53 | Qt::HANDLE QtLockedFile::getMutexHandle(int idx, bool doCreate)
54 | {
55 | if (mutexname.isEmpty()) {
56 | QFileInfo fi(*this);
57 | mutexname = QString::fromLatin1(MUTEX_PREFIX)
58 | + fi.absoluteFilePath().toLower();
59 | }
60 | QString mname(mutexname);
61 | if (idx >= 0)
62 | mname += QString::number(idx);
63 |
64 | Qt::HANDLE mutex;
65 | if (doCreate) {
66 | QT_WA( { mutex = CreateMutexW(NULL, FALSE, (TCHAR*)mname.utf16()); },
67 | { mutex = CreateMutexA(NULL, FALSE, mname.toLocal8Bit().constData()); } );
68 | if (!mutex) {
69 | qErrnoWarning("QtLockedFile::lock(): CreateMutex failed");
70 | return 0;
71 | }
72 | }
73 | else {
74 | QT_WA( { mutex = OpenMutexW(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, (TCHAR*)mname.utf16()); },
75 | { mutex = OpenMutexA(SYNCHRONIZE | MUTEX_MODIFY_STATE, FALSE, mname.toLocal8Bit().constData()); } );
76 | if (!mutex) {
77 | if (GetLastError() != ERROR_FILE_NOT_FOUND)
78 | qErrnoWarning("QtLockedFile::lock(): OpenMutex failed");
79 | return 0;
80 | }
81 | }
82 | return mutex;
83 | }
84 |
85 | bool QtLockedFile::waitMutex(Qt::HANDLE mutex, bool doBlock)
86 | {
87 | Q_ASSERT(mutex);
88 | DWORD res = WaitForSingleObject(mutex, doBlock ? INFINITE : 0);
89 | switch (res) {
90 | case WAIT_OBJECT_0:
91 | case WAIT_ABANDONED:
92 | return true;
93 | break;
94 | case WAIT_TIMEOUT:
95 | break;
96 | default:
97 | qErrnoWarning("QtLockedFile::lock(): WaitForSingleObject failed");
98 | }
99 | return false;
100 | }
101 |
102 |
103 |
104 | bool QtLockedFile::lock(LockMode mode, bool block)
105 | {
106 | if (!isOpen()) {
107 | qWarning("QtLockedFile::lock(): file is not opened");
108 | return false;
109 | }
110 |
111 | if (mode == NoLock)
112 | return unlock();
113 |
114 | if (mode == m_lock_mode)
115 | return true;
116 |
117 | if (m_lock_mode != NoLock)
118 | unlock();
119 |
120 | if (!wmutex && !(wmutex = getMutexHandle(-1, true)))
121 | return false;
122 |
123 | if (!waitMutex(wmutex, block))
124 | return false;
125 |
126 | if (mode == ReadLock) {
127 | int idx = 0;
128 | for (; idx < MAX_READERS; idx++) {
129 | rmutex = getMutexHandle(idx, false);
130 | if (!rmutex || waitMutex(rmutex, false))
131 | break;
132 | CloseHandle(rmutex);
133 | }
134 | bool ok = true;
135 | if (idx >= MAX_READERS) {
136 | qWarning("QtLockedFile::lock(): too many readers");
137 | rmutex = 0;
138 | ok = false;
139 | }
140 | else if (!rmutex) {
141 | rmutex = getMutexHandle(idx, true);
142 | if (!rmutex || !waitMutex(rmutex, false))
143 | ok = false;
144 | }
145 | if (!ok && rmutex) {
146 | CloseHandle(rmutex);
147 | rmutex = 0;
148 | }
149 | ReleaseMutex(wmutex);
150 | if (!ok)
151 | return false;
152 | }
153 | else {
154 | Q_ASSERT(rmutexes.isEmpty());
155 | for (int i = 0; i < MAX_READERS; i++) {
156 | Qt::HANDLE mutex = getMutexHandle(i, false);
157 | if (mutex)
158 | rmutexes.append(mutex);
159 | }
160 | if (rmutexes.size()) {
161 | DWORD res = WaitForMultipleObjects(rmutexes.size(), rmutexes.constData(),
162 | TRUE, block ? INFINITE : 0);
163 | if (res != WAIT_OBJECT_0 && res != WAIT_ABANDONED) {
164 | if (res != WAIT_TIMEOUT)
165 | qErrnoWarning("QtLockedFile::lock(): WaitForMultipleObjects failed");
166 | m_lock_mode = WriteLock; // trick unlock() to clean up - semiyucky
167 | unlock();
168 | return false;
169 | }
170 | }
171 | }
172 |
173 | m_lock_mode = mode;
174 | return true;
175 | }
176 |
177 | bool QtLockedFile::unlock()
178 | {
179 | if (!isOpen()) {
180 | qWarning("QtLockedFile::unlock(): file is not opened");
181 | return false;
182 | }
183 |
184 | if (!isLocked())
185 | return true;
186 |
187 | if (m_lock_mode == ReadLock) {
188 | ReleaseMutex(rmutex);
189 | CloseHandle(rmutex);
190 | rmutex = 0;
191 | }
192 | else {
193 | foreach(Qt::HANDLE mutex, rmutexes) {
194 | ReleaseMutex(mutex);
195 | CloseHandle(mutex);
196 | }
197 | rmutexes.clear();
198 | ReleaseMutex(wmutex);
199 | }
200 |
201 | m_lock_mode = QtLockedFile::NoLock;
202 | return true;
203 | }
204 |
205 | QtLockedFile::~QtLockedFile()
206 | {
207 | if (isOpen())
208 | unlock();
209 | if (wmutex)
210 | CloseHandle(wmutex);
211 | }
212 |
--------------------------------------------------------------------------------
/src/qtsingleapplication/qtlocalpeer.cpp:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | **
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 | ** Contact: http://www.qt-project.org/legal
5 | **
6 | ** This file is part of the Qt Solutions component.
7 | **
8 | ** $QT_BEGIN_LICENSE:BSD$
9 | ** You may use this file under the terms of the BSD license as follows:
10 | **
11 | ** "Redistribution and use in source and binary forms, with or without
12 | ** modification, are permitted provided that the following conditions are
13 | ** met:
14 | ** * Redistributions of source code must retain the above copyright
15 | ** notice, this list of conditions and the following disclaimer.
16 | ** * Redistributions in binary form must reproduce the above copyright
17 | ** notice, this list of conditions and the following disclaimer in
18 | ** the documentation and/or other materials provided with the
19 | ** distribution.
20 | ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 | ** of its contributors may be used to endorse or promote products derived
22 | ** from this software without specific prior written permission.
23 | **
24 | **
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 | **
37 | ** $QT_END_LICENSE$
38 | **
39 | ****************************************************************************/
40 |
41 |
42 | #include "qtlocalpeer.h"
43 | #include
44 | #include
45 |
46 | #if defined(Q_OS_WIN)
47 | #include
48 | #include
49 | typedef BOOL(WINAPI*PProcessIdToSessionId)(DWORD,DWORD*);
50 | static PProcessIdToSessionId pProcessIdToSessionId = 0;
51 | #endif
52 | #if defined(Q_OS_UNIX)
53 | #include
54 | #include
55 | #include
56 | #endif
57 |
58 | namespace QtLP_Private {
59 | #include "qtlockedfile.cpp"
60 | #if defined(Q_OS_WIN)
61 | #include "qtlockedfile_win.cpp"
62 | #else
63 | #include "qtlockedfile_unix.cpp"
64 | #endif
65 | }
66 |
67 | const char* QtLocalPeer::ack = "ack";
68 |
69 | QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId)
70 | : QObject(parent), id(appId)
71 | {
72 | QString prefix = id;
73 | if (id.isEmpty()) {
74 | id = QCoreApplication::applicationFilePath();
75 | #if defined(Q_OS_WIN)
76 | id = id.toLower();
77 | #endif
78 | prefix = id.section(QLatin1Char('/'), -1);
79 | }
80 | prefix.remove(QRegExp("[^a-zA-Z]"));
81 | prefix.truncate(6);
82 |
83 | QByteArray idc = id.toUtf8();
84 | quint16 idNum = qChecksum(idc.constData(), idc.size());
85 | socketName = QLatin1String("qtsingleapp-") + prefix
86 | + QLatin1Char('-') + QString::number(idNum, 16);
87 |
88 | #if defined(Q_OS_WIN)
89 | if (!pProcessIdToSessionId) {
90 | QLibrary lib("kernel32");
91 | pProcessIdToSessionId = (PProcessIdToSessionId)lib.resolve("ProcessIdToSessionId");
92 | }
93 | if (pProcessIdToSessionId) {
94 | DWORD sessionId = 0;
95 | pProcessIdToSessionId(GetCurrentProcessId(), &sessionId);
96 | socketName += QLatin1Char('-') + QString::number(sessionId, 16);
97 | }
98 | #else
99 | socketName += QLatin1Char('-') + QString::number(::getuid(), 16);
100 | #endif
101 |
102 | server = new QLocalServer(this);
103 | QString lockName = QDir(QDir::tempPath()).absolutePath()
104 | + QLatin1Char('/') + socketName
105 | + QLatin1String("-lockfile");
106 | lockFile.setFileName(lockName);
107 | lockFile.open(QIODevice::ReadWrite);
108 | }
109 |
110 |
111 |
112 | bool QtLocalPeer::isClient()
113 | {
114 | if (lockFile.isLocked())
115 | return false;
116 |
117 | if (!lockFile.lock(QtLP_Private::QtLockedFile::WriteLock, false))
118 | return true;
119 |
120 | bool res = server->listen(socketName);
121 | #if defined(Q_OS_UNIX) && (QT_VERSION >= QT_VERSION_CHECK(4,5,0))
122 | // ### Workaround
123 | if (!res && server->serverError() == QAbstractSocket::AddressInUseError) {
124 | QFile::remove(QDir::cleanPath(QDir::tempPath())+QLatin1Char('/')+socketName);
125 | res = server->listen(socketName);
126 | }
127 | #endif
128 | if (!res)
129 | qWarning("QtSingleCoreApplication: listen on local socket failed, %s", qPrintable(server->errorString()));
130 | QObject::connect(server, SIGNAL(newConnection()), SLOT(receiveConnection()));
131 | return false;
132 | }
133 |
134 |
135 | bool QtLocalPeer::sendMessage(const QString &message, int timeout)
136 | {
137 | if (!isClient())
138 | return false;
139 |
140 | QLocalSocket socket;
141 | bool connOk = false;
142 | for(int i = 0; i < 2; i++) {
143 | // Try twice, in case the other instance is just starting up
144 | socket.connectToServer(socketName);
145 | connOk = socket.waitForConnected(timeout/2);
146 | if (connOk || i)
147 | break;
148 | int ms = 250;
149 | #if defined(Q_OS_WIN)
150 | Sleep(DWORD(ms));
151 | #else
152 | struct timespec ts = { ms / 1000, (ms % 1000) * 1000 * 1000 };
153 | nanosleep(&ts, NULL);
154 | #endif
155 | }
156 | if (!connOk)
157 | return false;
158 |
159 | QByteArray uMsg(message.toUtf8());
160 | QDataStream ds(&socket);
161 | ds.writeBytes(uMsg.constData(), uMsg.size());
162 | bool res = socket.waitForBytesWritten(timeout);
163 | if (res) {
164 | res &= socket.waitForReadyRead(timeout); // wait for ack
165 | if (res)
166 | res &= (socket.read(qstrlen(ack)) == ack);
167 | }
168 | return res;
169 | }
170 |
171 |
172 | void QtLocalPeer::receiveConnection()
173 | {
174 | QLocalSocket* socket = server->nextPendingConnection();
175 | if (!socket)
176 | return;
177 |
178 | while (socket->bytesAvailable() < (int)sizeof(quint32))
179 | socket->waitForReadyRead();
180 | QDataStream ds(socket);
181 | QByteArray uMsg;
182 | quint32 remaining;
183 | ds >> remaining;
184 | uMsg.resize(remaining);
185 | int got = 0;
186 | char* uMsgBuf = uMsg.data();
187 | do {
188 | got = ds.readRawData(uMsgBuf, remaining);
189 | remaining -= got;
190 | uMsgBuf += got;
191 | } while (remaining && got >= 0 && socket->waitForReadyRead(2000));
192 | if (got < 0) {
193 | qWarning("QtLocalPeer: Message reception failed %s", socket->errorString().toLatin1().constData());
194 | delete socket;
195 | return;
196 | }
197 | QString message(QString::fromUtf8(uMsg));
198 | socket->write(ack, qstrlen(ack));
199 | socket->waitForBytesWritten(1000);
200 | socket->waitForDisconnected(1000); // make sure client reads ack
201 | delete socket;
202 | emit messageReceived(message); //### (might take a long time to return)
203 | }
204 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | qwx
2 | ===
3 |
4 | 微信Qt前端。
5 |
6 |
7 | ## 编译、运行
8 |
9 | ```
10 | qmake QWX_DEBUG=ON
11 | make
12 | ./qwx
13 | ```
14 |
15 |
16 | ## 网页微信客户端封包大全
17 |
18 | http://www.36kr.com/topics/4941
19 |
20 | 网页版微信功能只有一个:聊天。根据狼夜我这两天研究发现,网页版微信可以脱离手机微信,也就是手机微信退出、手机关机,都不影响网页端微信的在线以及聊天,关于如何使用加好友、朋友圈、摇一摇功能,我有个思路就是抓手机封包,然后使用,不过这个想法因为时间问题没有去实践,希望大家能研究出来的话在本页面留一个链接,十分感谢!
21 |
22 | 以下是Post/Get的封包大全,如果能看懂这个,基本上你就可以做出来了。
23 |
24 | 获取uuid
25 |
26 | https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_=1388994062250
27 |
28 | 获取二维码
29 |
30 | https://login.weixin.qq.com/qrcode/uuid?t=webwx+
31 |
32 | 等待扫描Get
33 |
34 | https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid=454d958c7f6243&tip=1&_=1388975894359
35 |
36 | https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid=454d958c7f6243&tip=1&_=1388975873359
37 |
38 | https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid=454d958c7f6243&tip=1&_=1388975883859
39 |
40 | 扫描了-返回
41 |
42 | window.code=201;
43 |
44 | 未扫描返回空
45 |
46 | 扫描之后-第一次请求成功
47 |
48 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxstatreport?type=1&r=1388975895453
49 |
50 | {"BaseRequest":{"Uin":0,"Sid":0},"Count":1,"List":[{"Type":1,"Text":"/cgi-bin/mmwebwx-bin/login, First Request Success, uuid: 454d958c7f6243"}]}
51 |
52 | 扫描之后-第二次请求开始
53 |
54 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxstatreport?type=1&r=1388975895453
55 |
56 | {"BaseRequest":{"Uin":0,"Sid":0},"Count":1,"List":[{"Type":1,"Text":"/cgi-bin/mmwebwx-bin/login, Second Request Start, uuid: 454d958c7f6243"}]}
57 |
58 | 等待确认Get
59 |
60 | https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid=454d958c7f6243&tip=0&_=1388975895453
61 |
62 | https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid=454d958c7f6243&tip=0&_=1388975900953
63 |
64 | https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid=454d958c7f6243&tip=0&_=1388975906453
65 |
66 | https://login.weixin.qq.com/cgi-bin/mmwebwx-bin/login?uuid=454d958c7f6243&tip=0&_=1388975911953
67 |
68 | 手机确认-返回
69 |
70 | window.code=200;
71 |
72 | window.redirect_uri="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket=03f725a8039d418ab79c69b6ffbd771b&lang=zh_CN&scan=1388975896";
73 |
74 | 未确认返回空
75 |
76 | get 登陆获取Cookie
77 |
78 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?ticket=03f725a8039d418ab79c69b6ffbd771b&lang=zh_CN&scan=1388975896&fun=new
79 |
80 | --设置Cookie 返回一个状态
81 |
82 | post 第二次请求成功
83 |
84 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxstatreport?type=1&r=1388976086218
85 |
86 | {"BaseRequest":{"Uin":0,"Sid":0},"Count":1,"List":[{"Type":1,"Text":"/cgi-bin/mmwebwx-bin/login, Second Request Success, uuid: 454d958c7f6243, time: 190765ms"}]}
87 |
88 | post 表示登陆成功-返回重要的数据key123
89 |
90 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=1388976086484
91 |
92 | {"BaseRequest":{"Uin":"750366800","Sid":"e75TXbI7TnKUevmI","Skey":"","DeviceID":"e519062714508114"}}
93 |
94 | post 可能是获取列表
95 |
96 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=e75TXbI7TnKUevmI&r=1388976086734
97 |
98 | {"BaseRequest":{"Uin":750366800,"Sid":"e75TXbI7TnKUevmI"},"SyncKey":{"Count":4,"List":[{"Key":1,"Val":620916854},{"Key":2,"Val":620917961},{"Key":3,"Val":620917948},{"Key":1000,"Val":1388967977}]},"rr":1388976086734}
99 |
100 | --这里的内容在上一步返回结果里
101 |
102 | post 可能是获取当前会话列表-大数据
103 |
104 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact?r=1388976086734
105 |
106 | {}
107 |
108 | post 可能是在手机上显示的提示信息
109 |
110 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify?r=1388976086750
111 |
112 | {"BaseRequest":{"Uin":750366800,"Sid":"e75TXbI7TnKUevmI","Skey":"","DeviceID":"e519062714508114"},"Code":3,"FromUserName":"langyeie","ToUserName":"langyeie","ClientMsgId":"1388976086750"}
113 |
114 | get 获取头像图片
115 |
116 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgeticon?seq=1388335457&username=langyeie
117 |
118 | get 同理可以获取其他微信好友的头像
119 |
120 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgeticon?seq=620917759&username=wxid_xx3mtgeux5511
121 |
122 | post 更改什么状态?标记已读?
123 |
124 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact?type=ex&r=1388976086812
125 |
126 | {"BaseRequest":{"Uin":750366800,"Sid":"e75TXbI7TnKUevmI","Skey":"","DeviceID":"e519062714508114"},"Count":10,"List":[{"UserName":"z_zer0v","ChatRoomId":3445229833},{"UserName":"huobao002","ChatRoomId":3445229833},{"UserName":"wxid_jo4qxoep4go411","ChatRoomId":3445229833},{"UserName":"jijunlong123456","ChatRoomId":3445229833},{"UserName":"wxid_toyaj4qwrynb21","ChatRoomId":3445229833},{"UserName":"wxid_6655286553012","ChatRoomId":3445229833},{"UserName":"wxid_rankrke1kkyd12","ChatRoomId":3445229833},{"UserName":"wxid_chcblpm846k022","ChatRoomId":3445229833},{"UserName":"tw297554396","ChatRoomId":3445229833},{"UserName":"wxid_3076050756212","ChatRoomId":3445229833}]}
127 |
128 | get headimg ?头像?
129 |
130 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetheadimg?seq=620917806&username=3445229833chatroom@
131 |
132 | get 监听会话
133 |
134 | https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?callback=jQuery18308660551080269895_1388975862078&r=1388976091937&sid=e75TXbI7TnKUevmI&uin=750366800&deviceid=e519062714508114&synckey=1_620916854%7C2_620917963%7C3_620917948%7C201_1388976090%7C1000_1388967977&_=1388976091937
135 |
136 | https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?callback=jQuery18308660551080269895_1388975862078&r=1388976119062&sid=e75TXbI7TnKUevmI&uin=750366800&deviceid=e519062714508114&synckey=1_620916854%7C2_620917963%7C3_620917948%7C201_1388976090%7C1000_1388967977&_=1388976119078
137 |
138 | https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?callback=jQuery18308660551080269895_1388975862078&r=1388976173375&sid=e75TXbI7TnKUevmI&uin=750366800&deviceid=e519062714508114&synckey=1_620916854%7C2_620917963%7C3_620917948%7C201_1388976090%7C1000_1388967977&_=1388976173390
139 |
140 | https://webpush.weixin.qq.com/cgi-bin/mmwebwx-bin/synccheck?callback=jQuery18308660551080269895_1388975862078&r=1388976146265&sid=e75TXbI7TnKUevmI&uin=750366800&deviceid=e519062714508114&synckey=1_620916854%7C2_620917963%7C3_620917948%7C201_1388976090%7C1000_1388967977&_=1388976146265
141 |
142 | 正常返回结果
143 |
144 | window.synccheck={retcode:"0",selector:"0"}
145 |
146 | 有消息返回结果
147 |
148 | window.synccheck={retcode:"0",selector:"6"}
149 |
150 | 发送消息返回结果
151 |
152 | window.synccheck={retcode:"0",selector:"2"}
153 |
154 | 朋友圈有动态
155 |
156 | window.synccheck={retcode:"0",selector:"4"}
157 |
158 | 获取消息-post-设置Cookie
159 |
160 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=e75TXbI7TnKUevmI&r=1388977398062
161 |
162 | {"BaseRequest":{"Uin":750366800,"Sid":"e75TXbI7TnKUevmI"},"SyncKey":{"Count":5,"List":[{"Key":1,"Val":620916854},{"Key":2,"Val":620917978},{"Key":3,"Val":620917975},{"Key":201,"Val":1388977392},{"Key":1000,"Val":1388967977}]},"rr":1388977398062}
163 |
164 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=e75TXbI7TnKUevmI&r=1388977583250
165 |
166 | {"BaseRequest":{"Uin":750366800,"Sid":"e75TXbI7TnKUevmI"},"SyncKey":{"Count":5,"List":[{"Key":1,"Val":620916854},{"Key":2,"Val":620917980},{"Key":3,"Val":620917975},{"Key":201,"Val":1388977400},{"Key":1000,"Val":1388967977}]},"rr":1388977583250}
167 |
168 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=e75TXbI7TnKUevmI&r=1388977660750
169 |
170 | {"BaseRequest":{"Uin":750366800,"Sid":"e75TXbI7TnKUevmI"},"SyncKey":{"Count":5,"List":[{"Key":1,"Val":620916854},{"Key":2,"Val":620917982},{"Key":3,"Val":620917975},{"Key":201,"Val":1388977585},{"Key":1000,"Val":1388967977}]},"rr":1388977660750}
171 |
172 | post 发送消息
173 |
174 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg?sid=e75TXbI7TnKUevmI&r=1388977830140
175 |
176 | {"BaseRequest":{"Uin":750366800,"Sid":"e75TXbI7TnKUevmI","Skey":"D6EBA5FA425CAE258F24E75CF51F2E1B4EEA9C5338BE456C","DeviceID":"e519062714508114"},"Msg":{"FromUserName":"langyeie","ToUserName":"pp80000","Type":1,"Content":"55","ClientMsgId":1388977830140,"LocalID":1388977830140}}
177 |
178 | https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync?sid=e75TXbI7TnKUevmI&r=1388977830390
179 |
180 | {"BaseRequest":{"Uin":750366800,"Sid":"e75TXbI7TnKUevmI"},"SyncKey":{"Count":5,"List":[{"Key":1,"Val":620916854},{"Key":2,"Val":620917986},{"Key":3,"Val":620917975},{"Key":201,"Val":1388977776},{"Key":1000,"Val":1388967977}]},"rr":1388977830390}
181 |
182 | get 有消息来,响铃
183 |
184 | https://res.wx.qq.com/zh_CN/htmledition/swf/msg17ced3.mp3
185 |
186 | 原文地址:http://www.langyeweb.com/Program/70.html,转载请保留出处。
187 |
188 |
189 |
--------------------------------------------------------------------------------
/src/qtsingleapplication/qtsingleapplication.cpp:
--------------------------------------------------------------------------------
1 | /****************************************************************************
2 | **
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 | ** Contact: http://www.qt-project.org/legal
5 | **
6 | ** This file is part of the Qt Solutions component.
7 | **
8 | ** $QT_BEGIN_LICENSE:BSD$
9 | ** You may use this file under the terms of the BSD license as follows:
10 | **
11 | ** "Redistribution and use in source and binary forms, with or without
12 | ** modification, are permitted provided that the following conditions are
13 | ** met:
14 | ** * Redistributions of source code must retain the above copyright
15 | ** notice, this list of conditions and the following disclaimer.
16 | ** * Redistributions in binary form must reproduce the above copyright
17 | ** notice, this list of conditions and the following disclaimer in
18 | ** the documentation and/or other materials provided with the
19 | ** distribution.
20 | ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 | ** of its contributors may be used to endorse or promote products derived
22 | ** from this software without specific prior written permission.
23 | **
24 | **
25 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 | **
37 | ** $QT_END_LICENSE$
38 | **
39 | ****************************************************************************/
40 |
41 |
42 | #include "qtsingleapplication.h"
43 | #include "qtlocalpeer.h"
44 | #include
45 |
46 |
47 | /*!
48 | \class QtSingleApplication qtsingleapplication.h
49 | \brief The QtSingleApplication class provides an API to detect and
50 | communicate with running instances of an application.
51 |
52 | This class allows you to create applications where only one
53 | instance should be running at a time. I.e., if the user tries to
54 | launch another instance, the already running instance will be
55 | activated instead. Another usecase is a client-server system,
56 | where the first started instance will assume the role of server,
57 | and the later instances will act as clients of that server.
58 |
59 | By default, the full path of the executable file is used to
60 | determine whether two processes are instances of the same
61 | application. You can also provide an explicit identifier string
62 | that will be compared instead.
63 |
64 | The application should create the QtSingleApplication object early
65 | in the startup phase, and call isRunning() to find out if another
66 | instance of this application is already running. If isRunning()
67 | returns false, it means that no other instance is running, and
68 | this instance has assumed the role as the running instance. In
69 | this case, the application should continue with the initialization
70 | of the application user interface before entering the event loop
71 | with exec(), as normal.
72 |
73 | The messageReceived() signal will be emitted when the running
74 | application receives messages from another instance of the same
75 | application. When a message is received it might be helpful to the
76 | user to raise the application so that it becomes visible. To
77 | facilitate this, QtSingleApplication provides the
78 | setActivationWindow() function and the activateWindow() slot.
79 |
80 | If isRunning() returns true, another instance is already
81 | running. It may be alerted to the fact that another instance has
82 | started by using the sendMessage() function. Also data such as
83 | startup parameters (e.g. the name of the file the user wanted this
84 | new instance to open) can be passed to the running instance with
85 | this function. Then, the application should terminate (or enter
86 | client mode).
87 |
88 | If isRunning() returns true, but sendMessage() fails, that is an
89 | indication that the running instance is frozen.
90 |
91 | Here's an example that shows how to convert an existing
92 | application to use QtSingleApplication. It is very simple and does
93 | not make use of all QtSingleApplication's functionality (see the
94 | examples for that).
95 |
96 | \code
97 | // Original
98 | int main(int argc, char **argv)
99 | {
100 | QApplication app(argc, argv);
101 |
102 | MyMainWidget mmw;
103 | mmw.show();
104 | return app.exec();
105 | }
106 |
107 | // Single instance
108 | int main(int argc, char **argv)
109 | {
110 | QtSingleApplication app(argc, argv);
111 |
112 | if (app.isRunning())
113 | return !app.sendMessage(someDataString);
114 |
115 | MyMainWidget mmw;
116 | app.setActivationWindow(&mmw);
117 | mmw.show();
118 | return app.exec();
119 | }
120 | \endcode
121 |
122 | Once this QtSingleApplication instance is destroyed (normally when
123 | the process exits or crashes), when the user next attempts to run the
124 | application this instance will not, of course, be encountered. The
125 | next instance to call isRunning() or sendMessage() will assume the
126 | role as the new running instance.
127 |
128 | For console (non-GUI) applications, QtSingleCoreApplication may be
129 | used instead of this class, to avoid the dependency on the QtGui
130 | library.
131 |
132 | \sa QtSingleCoreApplication
133 | */
134 |
135 |
136 | void QtSingleApplication::sysInit(const QString &appId)
137 | {
138 | actWin = 0;
139 | peer = new QtLocalPeer(this, appId);
140 | connect(peer, SIGNAL(messageReceived(const QString&)), SIGNAL(messageReceived(const QString&)));
141 | }
142 |
143 |
144 | /*!
145 | Creates a QtSingleApplication object. The application identifier
146 | will be QCoreApplication::applicationFilePath(). \a argc, \a
147 | argv, and \a GUIenabled are passed on to the QAppliation constructor.
148 |
149 | If you are creating a console application (i.e. setting \a
150 | GUIenabled to false), you may consider using
151 | QtSingleCoreApplication instead.
152 | */
153 |
154 | QtSingleApplication::QtSingleApplication(int &argc, char **argv, bool GUIenabled)
155 | : QApplication(argc, argv, GUIenabled)
156 | {
157 | sysInit();
158 | }
159 |
160 |
161 | /*!
162 | Creates a QtSingleApplication object with the application
163 | identifier \a appId. \a argc and \a argv are passed on to the
164 | QAppliation constructor.
165 | */
166 |
167 | QtSingleApplication::QtSingleApplication(const QString &appId, int &argc, char **argv)
168 | : QApplication(argc, argv)
169 | {
170 | sysInit(appId);
171 | }
172 |
173 | #if QT_VERSION < 0x050000
174 |
175 | /*!
176 | Creates a QtSingleApplication object. The application identifier
177 | will be QCoreApplication::applicationFilePath(). \a argc, \a
178 | argv, and \a type are passed on to the QAppliation constructor.
179 | */
180 | QtSingleApplication::QtSingleApplication(int &argc, char **argv, Type type)
181 | : QApplication(argc, argv, type)
182 | {
183 | sysInit();
184 | }
185 |
186 |
187 | # if defined(Q_WS_X11)
188 | /*!
189 | Special constructor for X11, ref. the documentation of
190 | QApplication's corresponding constructor. The application identifier
191 | will be QCoreApplication::applicationFilePath(). \a dpy, \a visual,
192 | and \a cmap are passed on to the QApplication constructor.
193 | */
194 | QtSingleApplication::QtSingleApplication(Display* dpy, Qt::HANDLE visual, Qt::HANDLE cmap)
195 | : QApplication(dpy, visual, cmap)
196 | {
197 | sysInit();
198 | }
199 |
200 | /*!
201 | Special constructor for X11, ref. the documentation of
202 | QApplication's corresponding constructor. The application identifier
203 | will be QCoreApplication::applicationFilePath(). \a dpy, \a argc, \a
204 | argv, \a visual, and \a cmap are passed on to the QApplication
205 | constructor.
206 | */
207 | QtSingleApplication::QtSingleApplication(Display *dpy, int &argc, char **argv, Qt::HANDLE visual, Qt::HANDLE cmap)
208 | : QApplication(dpy, argc, argv, visual, cmap)
209 | {
210 | sysInit();
211 | }
212 |
213 | /*!
214 | Special constructor for X11, ref. the documentation of
215 | QApplication's corresponding constructor. The application identifier
216 | will be \a appId. \a dpy, \a argc, \a
217 | argv, \a visual, and \a cmap are passed on to the QApplication
218 | constructor.
219 | */
220 | QtSingleApplication::QtSingleApplication(Display* dpy, const QString &appId, int argc, char **argv, Qt::HANDLE visual, Qt::HANDLE cmap)
221 | : QApplication(dpy, argc, argv, visual, cmap)
222 | {
223 | sysInit(appId);
224 | }
225 | # endif // Q_WS_X11
226 | #endif // QT_VERSION < 0x050000
227 |
228 |
229 | /*!
230 | Returns true if another instance of this application is running;
231 | otherwise false.
232 |
233 | This function does not find instances of this application that are
234 | being run by a different user (on Windows: that are running in
235 | another session).
236 |
237 | \sa sendMessage()
238 | */
239 |
240 | bool QtSingleApplication::isRunning()
241 | {
242 | return peer->isClient();
243 | }
244 |
245 |
246 | /*!
247 | Tries to send the text \a message to the currently running
248 | instance. The QtSingleApplication object in the running instance
249 | will emit the messageReceived() signal when it receives the
250 | message.
251 |
252 | This function returns true if the message has been sent to, and
253 | processed by, the current instance. If there is no instance
254 | currently running, or if the running instance fails to process the
255 | message within \a timeout milliseconds, this function return false.
256 |
257 | \sa isRunning(), messageReceived()
258 | */
259 | bool QtSingleApplication::sendMessage(const QString &message, int timeout)
260 | {
261 | return peer->sendMessage(message, timeout);
262 | }
263 |
264 |
265 | /*!
266 | Returns the application identifier. Two processes with the same
267 | identifier will be regarded as instances of the same application.
268 | */
269 | QString QtSingleApplication::id() const
270 | {
271 | return peer->applicationId();
272 | }
273 |
274 |
275 | /*!
276 | Sets the activation window of this application to \a aw. The
277 | activation window is the widget that will be activated by
278 | activateWindow(). This is typically the application's main window.
279 |
280 | If \a activateOnMessage is true (the default), the window will be
281 | activated automatically every time a message is received, just prior
282 | to the messageReceived() signal being emitted.
283 |
284 | \sa activateWindow(), messageReceived()
285 | */
286 |
287 | void QtSingleApplication::setActivationWindow(QWidget* aw, bool activateOnMessage)
288 | {
289 | actWin = aw;
290 | if (activateOnMessage)
291 | connect(peer, SIGNAL(messageReceived(const QString&)), this, SLOT(activateWindow()));
292 | else
293 | disconnect(peer, SIGNAL(messageReceived(const QString&)), this, SLOT(activateWindow()));
294 | }
295 |
296 |
297 | /*!
298 | Returns the applications activation window if one has been set by
299 | calling setActivationWindow(), otherwise returns 0.
300 |
301 | \sa setActivationWindow()
302 | */
303 | QWidget* QtSingleApplication::activationWindow() const
304 | {
305 | return actWin;
306 | }
307 |
308 |
309 | /*!
310 | De-minimizes, raises, and activates this application's activation window.
311 | This function does nothing if no activation window has been set.
312 |
313 | This is a convenience function to show the user that this
314 | application instance has been activated when he has tried to start
315 | another instance.
316 |
317 | This function should typically be called in response to the
318 | messageReceived() signal. By default, that will happen
319 | automatically, if an activation window has been set.
320 |
321 | \sa setActivationWindow(), messageReceived(), initialize()
322 | */
323 | void QtSingleApplication::activateWindow()
324 | {
325 | if (actWin) {
326 | actWin->setWindowState(actWin->windowState() & ~Qt::WindowMinimized);
327 | actWin->raise();
328 | actWin->activateWindow();
329 | }
330 | }
331 |
332 |
333 | /*!
334 | \fn void QtSingleApplication::messageReceived(const QString& message)
335 |
336 | This signal is emitted when the current instance receives a \a
337 | message from another instance of this application.
338 |
339 | \sa sendMessage(), setActivationWindow(), activateWindow()
340 | */
341 |
342 |
343 | /*!
344 | \fn void QtSingleApplication::initialize(bool dummy = true)
345 |
346 | \obsolete
347 | */
348 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 | {one line to give the program's name and a brief idea of what it does.}
635 | Copyright (C) {year} {name of author}
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | {project} Copyright (C) {year} {fullname}
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
676 |
--------------------------------------------------------------------------------