New qcma headless app.

Updated changelog.
Bumped version to 0.3.0.
This commit is contained in:
codestation
2014-03-28 10:07:13 -04:30
parent 6ed8dd749e
commit 80f19e5d23
27 changed files with 712 additions and 200 deletions

150
src/gui/clientmanager.cpp Normal file
View File

@@ -0,0 +1,150 @@
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2013 Codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "clientmanager.h"
#include "cmaclient.h"
#include "cmautils.h"
#include "forms/progressform.h"
#include <QSettings>
#include <vitamtp.h>
ClientManager::ClientManager(Database *db, QObject *parent) :
QObject(parent), m_db(db)
{
}
ClientManager::~ClientManager()
{
VitaMTP_Cleanup();
}
void ClientManager::databaseUpdated(int count)
{
progress.interruptShow();
progress.hide();
if(count >= 0) {
emit messageSent(tr("Added %1 items to the database").arg(count));
} else {
emit messageSent(tr("Database indexing aborted by user"));
}
}
void ClientManager::showPinDialog(QString name, int pin)
{
pinForm.setPin(name, pin);
pinForm.startCountdown();
}
void ClientManager::start()
{
if(VitaMTP_Init() < 0) {
emit messageSent(tr("Cannot initialize VitaMTP library"));
return;
}
// initializing database for the first use
refreshDatabase();
connect(m_db, SIGNAL(fileAdded(QString)), &progress, SLOT(setFileName(QString)));
connect(m_db, SIGNAL(directoryAdded(QString)), &progress, SLOT(setDirectoryName(QString)));
connect(m_db, SIGNAL(updated(int)), this, SLOT(databaseUpdated(int)));
connect(&progress, SIGNAL(canceled()), m_db, SLOT(cancelOperation()), Qt::DirectConnection);
thread_count = 0;
qDebug("Starting cma threads");
CmaClient *client;
QSettings settings;
if(!settings.value("disableUSB", false).toBool()) {
usb_thread = new QThread();
client = new CmaClient(m_db);
usb_thread->setObjectName("usb_thread");
connect(usb_thread, SIGNAL(started()), client, SLOT(connectUsb()));
connect(client, SIGNAL(messageSent(QString)), this, SIGNAL(messageSent(QString)));
connect(client, SIGNAL(finished()), usb_thread, SLOT(quit()), Qt::DirectConnection);
connect(usb_thread, SIGNAL(finished()), usb_thread, SLOT(deleteLater()));
connect(usb_thread, SIGNAL(finished()), this, SLOT(threadStopped()));
connect(usb_thread, SIGNAL(finished()), client, SLOT(deleteLater()));
connect(client, SIGNAL(deviceConnected(QString)), this, SIGNAL(deviceConnected(QString)));
connect(client, SIGNAL(deviceDisconnected()), this, SIGNAL(deviceDisconnected()));
connect(client, SIGNAL(refreshDatabase()), this, SLOT(refreshDatabase()));
client->moveToThread(usb_thread);
usb_thread->start();
thread_count++;
}
if(!settings.value("disableWireless", false).toBool()) {
wireless_thread = new QThread();
client = new CmaClient(m_db);
wireless_thread->setObjectName("wireless_thread");
connect(wireless_thread, SIGNAL(started()), client, SLOT(connectWireless()));
connect(client, SIGNAL(messageSent(QString)), this, SIGNAL(messageSent(QString)));
connect(client, SIGNAL(receivedPin(QString,int)), this, SLOT(showPinDialog(QString,int)));
connect(client, SIGNAL(finished()), wireless_thread, SLOT(quit()), Qt::DirectConnection);
connect(wireless_thread, SIGNAL(finished()), wireless_thread, SLOT(deleteLater()));
connect(wireless_thread, SIGNAL(finished()), this, SLOT(threadStopped()));
connect(wireless_thread, SIGNAL(finished()), client, SLOT(deleteLater()));
connect(client, SIGNAL(pinComplete()), &pinForm, SLOT(hide()));
connect(client, SIGNAL(deviceConnected(QString)), this, SIGNAL(deviceConnected(QString)));
connect(client, SIGNAL(deviceDisconnected()), this, SIGNAL(deviceDisconnected()));
connect(client, SIGNAL(refreshDatabase()), this, SLOT(refreshDatabase()));
client->moveToThread(wireless_thread);
wireless_thread->start();
thread_count++;
}
if(thread_count == 0) {
emit messageSent(tr("You must enable at least USB or Wireless monitoring"));
}
}
void ClientManager::refreshDatabase()
{
if(m_db->load()) {
return;
}
if(!m_db->rescan()) {
emit messageSent(tr("No PS Vita system has been registered"));
} else {
progress.showDelayed(1000);
}
}
void ClientManager::stop()
{
if(CmaClient::stop() < 0) {
emit stopped();
}
}
void ClientManager::threadStopped()
{
mutex.lock();
if(--thread_count == 0) {
emit stopped();
}
mutex.unlock();
}

66
src/gui/clientmanager.h Normal file
View File

@@ -0,0 +1,66 @@
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2013 Codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CLIENTMANAGER_H
#define CLIENTMANAGER_H
#include "database.h"
#include "forms/pinform.h"
#include "forms/progressform.h"
#include <QObject>
#include <QThread>
class ClientManager : public QObject
{
Q_OBJECT
public:
explicit ClientManager(Database *db, QObject *parent = 0);
~ClientManager();
void start();
void stop();
private:
int thread_count;
QMutex mutex;
Database *m_db;
PinForm pinForm;
ProgressForm progress;
QThread *usb_thread;
QThread *wireless_thread;
signals:
void stopped();
void receivedPin(int);
void deviceDisconnected();
void messageSent(QString);
void deviceConnected(QString);
private slots:
void threadStopped();
void refreshDatabase();
void databaseUpdated(int count);
void showPinDialog(QString name, int pin);
};
#endif // CLIENTMANAGER_H

View File

@@ -0,0 +1,89 @@
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2013 Xian Nox
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "filterlineedit.h"
#include <QIcon>
#include <QStyle>
FilterLineEdit::FilterLineEdit(QWidget *parent) :
QLineEdit(parent)
{
int frame_width = frameWidth();
clearButton = new QToolButton(this);
QIcon clearIcon(":/main/resources/images/edit-clear-locationbar-rtl.png");
clearButton->setIcon(clearIcon);
clearButton->setIconSize(QSize(sizeHint().height() - 4 * frame_width,
sizeHint().height() - 4 * frame_width));
clearButton->setCursor(Qt::ArrowCursor);
clearButton->setStyleSheet("QToolButton { border:none; padding:0px; }");
clearButton->hide();
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(updateCloseButton(const QString&)));
setStyleSheet(QString("LineEdit { color:black; font-style:normal; padding-right:%1px; }").arg(
clearButton->sizeHint().width() + frame_width + 1));
QSize min_size_hint = minimumSizeHint();
setMinimumSize(qMax(min_size_hint.width(), clearButton->sizeHint().height() + frame_width),
qMax(min_size_hint.height(), clearButton->sizeHint().height() + frame_width));
}
void FilterLineEdit::updateCloseButton(const QString& text)
{
if(text.isEmpty() || text == tr("Filter")) {
clearButton->setVisible(false);
} else {
clearButton->setVisible(true);
}
}
void FilterLineEdit::focusInEvent(QFocusEvent *e)
{
if(this->styleSheet() == "FilterLineEdit { color:gray; font-style:italic; }") {
this->clear();
}
setStyleSheet(QString("FilterLineEdit { color:black; font-style:normal; padding-right:%1px; }").arg(clearButton->sizeHint().width() + frameWidth() + 1));
QLineEdit::focusInEvent(e);
}
void FilterLineEdit::focusOutEvent(QFocusEvent *e)
{
if(this->text().isEmpty()) {
this->setText(tr("Filter"));
this->setStyleSheet("FilterLineEdit { color:gray; font-style:italic; }");
}
QLineEdit::focusOutEvent(e);
}
int FilterLineEdit::frameWidth() const
{
return style()->pixelMetric(QStyle::PM_DefaultFrameWidth, 0, this);
}
void FilterLineEdit::resizeEvent(QResizeEvent *e)
{
QSize sz = clearButton->sizeHint();
clearButton->move(rect().right() - sz.width(), rect().bottom() - sz.height() + frameWidth());
QLineEdit::resizeEvent(e);
}

47
src/gui/filterlineedit.h Normal file
View File

@@ -0,0 +1,47 @@
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2013 Xian Nox
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef FILTERLINEEDIT_H
#define FILTERLINEEDIT_H
#include <QLineEdit>
#include <QToolButton>
class FilterLineEdit : public QLineEdit
{
Q_OBJECT
public:
explicit FilterLineEdit(QWidget *parent = 0);
protected:
void focusInEvent(QFocusEvent *e);
void focusOutEvent(QFocusEvent *e);
void resizeEvent(QResizeEvent *e);
int frameWidth() const;
private:
QToolButton *clearButton;
private slots:
void updateCloseButton(const QString &text);
};
#endif // FILTERLINEEDIT_H

25
src/gui/kdenotifier.cpp Normal file
View File

@@ -0,0 +1,25 @@
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2014 Codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "kdenotifier.h"
KDENotifier::KDENotifier(const QString &id, QObject *parent) :
KStatusNotifierItem(id, parent)
{
}

41
src/gui/kdenotifier.h Normal file
View File

@@ -0,0 +1,41 @@
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2014 Codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KDENOTIFIER_H
#define KDENOTIFIER_H
#include <kstatusnotifieritem.h>
#include <kmenu.h>
class KDENotifier : public KStatusNotifierItem
{
Q_OBJECT
public:
explicit KDENotifier(const QString &id, QObject *parent = 0);
signals:
public slots:
// block left click because it shows the default widget
virtual void activate (const QPoint &pos=QPoint()) {
Q_UNUSED(pos);
}
};
#endif // KDENOTIFIER_H

120
src/gui/main.cpp Normal file
View File

@@ -0,0 +1,120 @@
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2013 Codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Q_OS_WIN32
#include <signal.h>
#endif
#include <QDebug>
#include <QLibraryInfo>
#include <QLocale>
#include <QTextCodec>
#include <QThread>
#include <QTranslator>
#include <inttypes.h>
#include "singleapplication.h"
#include "mainwidget.h"
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
void noMessageOutput(QtMsgType type, const QMessageLogContext &, const QString & str)
{
const char * msg = str.toStdString().c_str();
#else
void noMessageOutput(QtMsgType type, const char *msg)
{
#endif
Q_UNUSED(type);
Q_UNUSED(msg);
}
int main(int argc, char *argv[])
{
if(SingleApplication::sendMessage(QObject::tr("A instance of QCMA is already running"))) {
return 0;
}
SingleApplication app(argc, argv);
#ifndef Q_OS_WIN32
// FIXME: libmtp sends SIGPIPE if a socket write fails crashing the whole app
// the proper fix is to libmtp to handle the cancel properly or ignoring
// SIGPIPE on the socket
signal(SIGPIPE, SIG_IGN);
#endif
if(app.arguments().contains("--with-debug")) {
VitaMTP_Set_Logging(VitaMTP_DEBUG);
} else if(app.arguments().contains("--verbose")) {
VitaMTP_Set_Logging(VitaMTP_VERBOSE);
} else {
VitaMTP_Set_Logging(VitaMTP_NONE);
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
qInstallMessageHandler(noMessageOutput);
#else
qInstallMsgHandler(noMessageOutput);
#endif
}
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
#endif
qDebug("Starting QCMA %s", QCMA_VER);
QTranslator translator;
QString locale = QLocale().system().name();
qDebug() << "Current locale:" << locale;
if(app.arguments().contains("--set-locale")) {
int index = app.arguments().indexOf("--set-locale");
if(index + 1 < app.arguments().length()) {
qDebug("Enforcing locale: %s", app.arguments().at(index + 1).toUtf8().data());
locale = app.arguments().at(index + 1);
}
}
if(translator.load("qcma_" + locale, ":/resources/translations")) {
app.installTranslator(&translator);
} else {
qDebug() << "Cannot load translation for locale:" << locale;
}
QTranslator system_translator;
system_translator.load("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
app.installTranslator(&system_translator);
qDebug("Starting main thread: 0x%016" PRIxPTR, (uintptr_t)QThread::currentThreadId());
// set the organization/application for QSettings to work properly
app.setOrganizationName("qcma");
app.setApplicationName("qcma");
//TODO: check if this is actually needed since we don't have a main window by default
QApplication::setQuitOnLastWindowClosed(false);
MainWidget widget;
widget.prepareApplication();
// receive the message from another process
QObject::connect(&app, SIGNAL(messageAvailable(QString)), &widget, SLOT(receiveMessage(QString)));
return app.exec();
}

271
src/gui/mainwidget.cpp Normal file
View File

@@ -0,0 +1,271 @@
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2013 Codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mainwidget.h"
#include "cmaclient.h"
#include "cmautils.h"
#include "qlistdb.h"
#include "sqlitedb.h"
#include <QAction>
#include <QApplication>
#include <QDebug>
#include <QDir>
#include <QGridLayout>
#include <QMenu>
#include <QMessageBox>
#include <QSettings>
#include <QTimer>
#include <QSettings>
#include <QSpacerItem>
#ifdef ENABLE_KDE_NOTIFIER
#include <kmenu.h>
#endif
const QStringList MainWidget::path_list = QStringList() << "photoPath" << "musicPath" << "videoPath" << "appsPath" << "urlPath";
bool sleptOnce = false;
MainWidget::MainWidget(QWidget *parent) :
QWidget(parent), db(NULL), configForm(NULL), managerForm(NULL), backupForm(NULL)
{
}
void MainWidget::checkSettings()
{
QSettings settings;
// make sure that all the paths are set, else show the config dialog
foreach(const QString &path, path_list) {
if(!settings.contains(path)) {
first_run = true;
configForm->show();
return;
}
}
first_run = false;
managerForm->start();
}
void MainWidget::dialogResult(int result)
{
if(result == QDialog::Accepted) {
if(first_run) {
first_run = false;
managerForm->start();
}
} else if(first_run) {
qApp->quit();
}
}
void MainWidget::stopServer()
{
setTrayTooltip(tr("Shutting down..."));
if(CmaClient::isRunning()) {
receiveMessage(tr("Stopping QCMA (disconnect your PS Vita)"));
}
managerForm->stop();
}
void MainWidget::deviceDisconnect()
{
#ifndef ENABLE_KDE_NOTIFIER
#ifndef Q_OS_WIN32
trayIcon->setIcon(QIcon(":/main/resources/images/psv_icon_dc.png"));
#else
trayIcon->setIcon(QIcon(":/main/resources/images/psv_icon_16_dc.png"));
#endif
#else
notifierItem->setIconByPixmap(QIcon(":/main/resources/images/psv_icon_dc.png"));
#endif
qDebug("Icon changed - disconnected");
setTrayTooltip(tr("Disconnected"));
receiveMessage(tr("The device has been disconnected"));
}
void MainWidget::deviceConnected(QString message)
{
#ifndef ENABLE_KDE_NOTIFIER
#ifndef Q_OS_WIN32
trayIcon->setIcon(QIcon(":/main/resources/images/psv_icon.png"));
#else
trayIcon->setIcon(QIcon(":/main/resources/images/psv_icon_16.png"));
#endif
#else
notifierItem->setIconByPixmap(QIcon(":/main/resources/images/psv_icon.png"));
#endif
qDebug("Icon changed - connected");
setTrayTooltip(message);
receiveMessage(message);
}
void MainWidget::prepareApplication()
{
//TODO: delete database before exit
if(QSettings().value("useMemoryStorage", true).toBool()) {
db = new QListDB();
} else {
db = new SQLiteDB();
}
configForm = new ConfigWidget(this);
backupForm = new BackupManagerForm(db, this);
managerForm = new ClientManager(db, this);
connectSignals();
createTrayIcon();
checkSettings();
}
void MainWidget::connectSignals()
{
connect(configForm, SIGNAL(finished(int)), this, SLOT(dialogResult(int)));
connect(managerForm, SIGNAL(stopped()), qApp, SLOT(quit()));
connect(managerForm, SIGNAL(deviceConnected(QString)), this, SLOT(deviceConnected(QString)));
connect(managerForm, SIGNAL(deviceDisconnected()), this, SLOT(deviceDisconnect()));
connect(managerForm, SIGNAL(messageSent(QString)), this, SLOT(receiveMessage(QString)));
//backupForm.db = managerForm.db;
}
void MainWidget::setTrayTooltip(QString message)
{
#ifndef ENABLE_KDE_NOTIFIER
trayIcon->setToolTip(message);
#else
notifierItem->setToolTipSubTitle(message);
#endif
}
void MainWidget::openManager()
{
backupForm->loadBackupListing(-1);
backupForm->show();
}
void MainWidget::showAboutDialog()
{
QMessageBox about;
about.setText(QString("QCMA ") + QCMA_VER);
about.setWindowTitle(tr("About QCMA"));
#ifndef QCMA_BUILD_HASH
about.setInformativeText(tr("Copyright (C) 2013 Codestation") + "\n");
#else
about.setInformativeText(tr("Copyright (C) 2013 Codestation\n\nbuild hash: %1\nbuild branch: %2").arg(QCMA_BUILD_HASH).arg(QCMA_BUILD_BRANCH));
#endif
about.setStandardButtons(QMessageBox::Ok);
about.setIconPixmap(QPixmap(":/main/resources/images/qcma.png"));
about.setDefaultButton(QMessageBox::Ok);
// hack to expand the messagebox minimum size
QSpacerItem* horizontalSpacer = new QSpacerItem(300, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
QGridLayout* layout = (QGridLayout*)about.layout();
layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());
about.show();
about.exec();
}
void MainWidget::showAboutQt()
{
QMessageBox::aboutQt(this);
}
void MainWidget::createTrayIcon()
{
options = new QAction(tr("&Settings"), this);
reload = new QAction(tr("&Refresh database"), this);
backup = new QAction(tr("&Backup Manager"), this);
about = new QAction(tr("&About QCMA"), this);
about_qt = new QAction(tr("Abou&t Qt"), this);
quit = new QAction(tr("&Quit"), this);
connect(options, SIGNAL(triggered()), configForm, SLOT(open()));
connect(backup, SIGNAL(triggered()), this, SLOT(openManager()));
connect(reload, SIGNAL(triggered()), managerForm, SLOT(refreshDatabase()));
connect(about, SIGNAL(triggered()), this, SLOT(showAboutDialog()));
connect(about_qt, SIGNAL(triggered()), this, SLOT(showAboutQt()));
connect(quit, SIGNAL(triggered()), this, SLOT(stopServer()));
#ifndef ENABLE_KDE_NOTIFIER
QMenu *trayIconMenu = new QMenu(this);
#else
KMenu *trayIconMenu = new KMenu(this);
#endif
trayIconMenu->addAction(options);
trayIconMenu->addAction(reload);
trayIconMenu->addAction(backup);
trayIconMenu->addSeparator();
trayIconMenu->addAction(about);
trayIconMenu->addAction(about_qt);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quit);
#ifndef ENABLE_KDE_NOTIFIER
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
#ifndef Q_OS_WIN32
trayIcon->setIcon(QIcon(":/main/resources/images/psv_icon_dc.png"));
#else
trayIcon->setIcon(QIcon(":/main/resources/images/psv_icon_16_dc.png"));
#endif
trayIcon->show();
#else
notifierItem = new KDENotifier("QcmaNotifier", this);
notifierItem->setContextMenu(trayIconMenu);
notifierItem->setTitle("Qcma");
notifierItem->setCategory(KStatusNotifierItem::ApplicationStatus);
notifierItem->setIconByPixmap(QIcon(":/main/resources/images/psv_icon_dc.png"));
notifierItem->setStatus(KStatusNotifierItem::Active);
notifierItem->setToolTipTitle(tr("Qcma status"));
notifierItem->setToolTipIconByPixmap(QIcon(":/main/resources/images/qcma.png"));
notifierItem->setToolTipSubTitle(tr("Disconnected"));
notifierItem->setStandardActionsEnabled(false);
#endif
}
void MainWidget::receiveMessage(QString message)
{
// a timeout is added before the popups are displayed to prevent them from
// appearing in the wrong location
if(!sleptOnce) {
Sleeper::sleep(1);
sleptOnce = true;
}
#ifndef ENABLE_KDE_NOTIFIER
if(trayIcon->isVisible()) {
trayIcon->showMessage(tr("Information"), message);
}
#else
notifierItem->showMessage(tr("Qcma - Information"), message, "dialog-information", 3000);
#endif
}
MainWidget::~MainWidget()
{
#ifndef ENABLE_KDE_NOTIFIER
trayIcon->hide();
#endif
delete db;
}

91
src/gui/mainwidget.h Normal file
View File

@@ -0,0 +1,91 @@
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2013 Codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include "cmaclient.h"
#include "clientmanager.h"
#include "forms/configwidget.h"
#include "forms/backupmanagerform.h"
#include "forms/progressform.h"
#include <QAction>
#include <QWidget>
#include <QSystemTrayIcon>
#ifdef ENABLE_KDE_NOTIFIER
#include "kdenotifier.h"
#endif
#include <vitamtp.h>
class MainWidget : public QWidget
{
Q_OBJECT
public:
explicit MainWidget(QWidget *parent = 0);
~MainWidget();
void prepareApplication();
private:
void connectSignals();
void createTrayIcon();
void checkSettings();
bool first_run;
// database
Database *db;
// forms
ConfigWidget *configForm;
ClientManager *managerForm;
BackupManagerForm *backupForm;
//system tray
QAction *quit;
QAction *reload;
QAction *options;
QAction *backup;
QAction *about;
QAction *about_qt;
#ifndef ENABLE_KDE_NOTIFIER
QSystemTrayIcon *trayIcon;
#else
KDENotifier *notifierItem;
#endif
const static QStringList path_list;
private slots:
void stopServer();
void openManager();
void showAboutQt();
void showAboutDialog();
void deviceConnected(QString message);
void deviceDisconnect();
void dialogResult(int result);
void receiveMessage(QString message);
void setTrayTooltip(QString message);
};
#endif // MAINWIDGET_H

View File

@@ -0,0 +1,69 @@
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2013 Codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "singleapplication.h"
#include <QDebug>
const int SingleApplication::timeout = 500;
const QString SingleApplication::SHARED_KEY = "QCMA_KEY";
SingleApplication::SingleApplication(int &argc, char **argv) :
QApplication(argc, argv)
{
server = new QLocalServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(receiveMessage()));
QLocalServer::removeServer(SHARED_KEY);
server->listen(SHARED_KEY);
}
void SingleApplication::receiveMessage()
{
QLocalSocket *socket = server->nextPendingConnection();
if(!socket->waitForReadyRead(timeout)) {
qDebug() << socket->errorString();
return;
}
QByteArray byteArray = socket->readAll();
QString message = QString::fromUtf8(byteArray.constData());
emit messageAvailable(message);
socket->disconnectFromServer();
}
bool SingleApplication::sendMessage(const QString &message)
{
QLocalSocket socket;
socket.connectToServer(SHARED_KEY, QIODevice::WriteOnly);
if(!socket.waitForConnected(timeout)) {
return false;
}
socket.write(message.toUtf8());
if(!socket.waitForBytesWritten(timeout)) {
qDebug() << socket.errorString();
return false;
}
socket.disconnectFromServer();
return true;
}

View File

@@ -0,0 +1,50 @@
/*
* QCMA: Cross-platform content manager assistant for the PS Vita
*
* Copyright (C) 2013 Codestation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SINGLEAPPLICATION_H
#define SINGLEAPPLICATION_H
#include <QApplication>
#include <QLocalSocket>
#include <QLocalServer>
#include <QSharedMemory>
class SingleApplication : public QApplication
{
Q_OBJECT
public:
explicit SingleApplication(int &argc, char **argv);
static bool sendMessage(const QString &message);
private:
QLocalServer *server;
static const int timeout;
static const QString SHARED_KEY;
signals:
void messageAvailable(QString message);
public slots:
void receiveMessage();
};
#endif // SINGLEAPPLICATION_H