Implemented android module startup with default settings.
Simplified main_android.
This commit is contained in:
		@@ -3,6 +3,7 @@ include(../common/defines.pri)
 | 
			
		||||
 | 
			
		||||
TARGET = qcma_android
 | 
			
		||||
TEMPLATE=app
 | 
			
		||||
QT += network sql
 | 
			
		||||
LIBS += -L../common -lqcma_common
 | 
			
		||||
 | 
			
		||||
# this library needs to link statically their deps but Qt doesn't pass --static to PKGCONFIG
 | 
			
		||||
@@ -10,11 +11,11 @@ QMAKE_CXXFLAGS += $$system(pkg-config --static --cflags libvitamtp libavformat l
 | 
			
		||||
LIBS += $$system(pkg-config --static --libs libvitamtp libavformat libavcodec libavutil libswscale)
 | 
			
		||||
 | 
			
		||||
SOURCES += \
 | 
			
		||||
           main_android.cpp
 | 
			
		||||
#           headlessmanager.cpp
 | 
			
		||||
    main_android.cpp \
 | 
			
		||||
    sevicemanager.cpp
 | 
			
		||||
 | 
			
		||||
#HEADERS += \
 | 
			
		||||
#           headlessmanager.h
 | 
			
		||||
HEADERS += \
 | 
			
		||||
    servicemanager.h
 | 
			
		||||
 | 
			
		||||
DISTFILES += \
 | 
			
		||||
    android-src/gradle/wrapper/gradle-wrapper.jar \
 | 
			
		||||
 
 | 
			
		||||
@@ -29,62 +29,25 @@
 | 
			
		||||
#include <inttypes.h>
 | 
			
		||||
#include <vitamtp.h>
 | 
			
		||||
 | 
			
		||||
#include "headlessmanager.h"
 | 
			
		||||
 | 
			
		||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
 | 
			
		||||
static void noDebugOutput(QtMsgType type, const QMessageLogContext &, const QString & msg)
 | 
			
		||||
{
 | 
			
		||||
    QByteArray localMsg = msg.toLocal8Bit();
 | 
			
		||||
    const char *message = localMsg.constData();
 | 
			
		||||
#else
 | 
			
		||||
static void noDebugOutput(QtMsgType type, const char *message)
 | 
			
		||||
{
 | 
			
		||||
#endif
 | 
			
		||||
    switch (type) {
 | 
			
		||||
    case QtDebugMsg:
 | 
			
		||||
        break;
 | 
			
		||||
    case QtWarningMsg:
 | 
			
		||||
        fprintf(stderr, "Warning: %s\n", message);
 | 
			
		||||
        break;
 | 
			
		||||
    case QtCriticalMsg:
 | 
			
		||||
        fprintf(stderr, "Critical: %s\n", message);
 | 
			
		||||
        break;
 | 
			
		||||
    case QtFatalMsg:
 | 
			
		||||
        fprintf(stderr, "Fatal: %s\n", message);
 | 
			
		||||
        abort();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
#include "servicemanager.h"
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
    QCoreApplication app(argc, argv);
 | 
			
		||||
    app.setApplicationName("Qcma");
 | 
			
		||||
 | 
			
		||||
    // 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);
 | 
			
		||||
 | 
			
		||||
    if(app.arguments().contains("--with-debug")) {
 | 
			
		||||
        VitaMTP_Set_Logging(VitaMTP_DEBUG);
 | 
			
		||||
    } else if(app.arguments().contains("--verbose")) {
 | 
			
		||||
        VitaMTP_Set_Logging(VitaMTP_VERBOSE);
 | 
			
		||||
    } else {
 | 
			
		||||
    // stdout goes to /dev/null on android
 | 
			
		||||
    VitaMTP_Set_Logging(VitaMTP_NONE);
 | 
			
		||||
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
 | 
			
		||||
        qInstallMessageHandler(noDebugOutput);
 | 
			
		||||
#else
 | 
			
		||||
        qInstallMsgHandler(noDebugOutput);
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
 | 
			
		||||
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    QTextStream(stdout) << "Starting Qcma " << QCMA_VER << endl;
 | 
			
		||||
    qDebug("Starting Qcma %s", QCMA_VER);
 | 
			
		||||
 | 
			
		||||
    QTranslator translator;
 | 
			
		||||
    QString locale = QLocale().system().name();
 | 
			
		||||
    QString locale = "en"; //QLocale().system().name();
 | 
			
		||||
    qDebug() << "Current locale:" << locale;
 | 
			
		||||
 | 
			
		||||
    if(app.arguments().contains("--set-locale")) {
 | 
			
		||||
@@ -111,12 +74,8 @@ int main(int argc, char *argv[])
 | 
			
		||||
    app.setOrganizationName("codestation");
 | 
			
		||||
    app.setApplicationName("qcma");
 | 
			
		||||
 | 
			
		||||
    //HeadlessManager manager;
 | 
			
		||||
 | 
			
		||||
    // receive the message from another process
 | 
			
		||||
    //QObject::connect(&app, SIGNAL(messageAvailable(QString)), &manager, SLOT(receiveMessage(QString)));
 | 
			
		||||
 | 
			
		||||
    //manager.start();
 | 
			
		||||
    ServiceManager manager;
 | 
			
		||||
    manager.start();
 | 
			
		||||
 | 
			
		||||
    return app.exec();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -17,30 +17,27 @@
 | 
			
		||||
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef HEADLESSMANAGER_H
 | 
			
		||||
#define HEADLESSMANAGER_H
 | 
			
		||||
#ifndef SERVICEMANAGER_H
 | 
			
		||||
#define SERVICEMANAGER_H
 | 
			
		||||
 | 
			
		||||
#include "database.h"
 | 
			
		||||
 | 
			
		||||
#include <QObject>
 | 
			
		||||
#include <QThread>
 | 
			
		||||
#include <QSocketNotifier>
 | 
			
		||||
 | 
			
		||||
class HeadlessManager : public QObject
 | 
			
		||||
class ServiceManager : public QObject
 | 
			
		||||
{
 | 
			
		||||
    Q_OBJECT
 | 
			
		||||
 | 
			
		||||
public:
 | 
			
		||||
    explicit HeadlessManager(QObject *parent = 0);
 | 
			
		||||
    ~HeadlessManager();
 | 
			
		||||
    explicit ServiceManager(QObject *parent = 0);
 | 
			
		||||
    ~ServiceManager();
 | 
			
		||||
 | 
			
		||||
    void start();
 | 
			
		||||
 | 
			
		||||
    // unix signal handlers
 | 
			
		||||
    static void hupSignalHandler(int);
 | 
			
		||||
    static void termSignalHandler(int);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    void loadDefaultSettings();
 | 
			
		||||
 | 
			
		||||
    int thread_count;
 | 
			
		||||
    QMutex mutex;
 | 
			
		||||
 | 
			
		||||
@@ -49,13 +46,6 @@ private:
 | 
			
		||||
    QThread *usb_thread;
 | 
			
		||||
    QThread *wireless_thread;
 | 
			
		||||
 | 
			
		||||
    // signal handling
 | 
			
		||||
    static int sighup_fd[2];
 | 
			
		||||
    static int sigterm_fd[2];
 | 
			
		||||
 | 
			
		||||
    QSocketNotifier *sn_hup;
 | 
			
		||||
    QSocketNotifier *sn_term;
 | 
			
		||||
 | 
			
		||||
signals:
 | 
			
		||||
    void stopped();
 | 
			
		||||
    void databaseUpdated(int count);
 | 
			
		||||
@@ -64,13 +54,9 @@ public slots:
 | 
			
		||||
    void refreshDatabase();
 | 
			
		||||
    void stop();
 | 
			
		||||
 | 
			
		||||
    // Qt signal handlers
 | 
			
		||||
    void handleSigHup();
 | 
			
		||||
    void handleSigTerm();
 | 
			
		||||
 | 
			
		||||
private slots:
 | 
			
		||||
    void threadStopped();
 | 
			
		||||
    void receiveMessage(QString message);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif // HEADLESSMANAGER_H
 | 
			
		||||
#endif // SERVICEMANAGER_H
 | 
			
		||||
@@ -21,40 +21,27 @@
 | 
			
		||||
#include "cmautils.h"
 | 
			
		||||
#include "sqlitedb.h"
 | 
			
		||||
#include "qlistdb.h"
 | 
			
		||||
#include "headlessmanager.h"
 | 
			
		||||
#include "servicemanager.h"
 | 
			
		||||
 | 
			
		||||
#include <QCoreApplication>
 | 
			
		||||
#include <QDir>
 | 
			
		||||
#include <QSettings>
 | 
			
		||||
#include <QStandardPaths>
 | 
			
		||||
#include <QTextStream>
 | 
			
		||||
#include <sys/socket.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#include <vitamtp.h>
 | 
			
		||||
 | 
			
		||||
int HeadlessManager::sighup_fd[2];
 | 
			
		||||
int HeadlessManager::sigterm_fd[2];
 | 
			
		||||
 | 
			
		||||
HeadlessManager::HeadlessManager(QObject *obj_parent) :
 | 
			
		||||
ServiceManager::ServiceManager(QObject *obj_parent) :
 | 
			
		||||
    QObject(obj_parent)
 | 
			
		||||
{
 | 
			
		||||
    if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sighup_fd))
 | 
			
		||||
       qFatal("Couldn't create HUP socketpair");
 | 
			
		||||
 | 
			
		||||
    if (::socketpair(AF_UNIX, SOCK_STREAM, 0, sigterm_fd))
 | 
			
		||||
       qFatal("Couldn't create TERM socketpair");
 | 
			
		||||
 | 
			
		||||
    sn_hup = new QSocketNotifier(sighup_fd[1], QSocketNotifier::Read, this);
 | 
			
		||||
    connect(sn_hup, SIGNAL(activated(int)), this, SLOT(handleSigHup()));
 | 
			
		||||
    sn_term = new QSocketNotifier(sigterm_fd[1], QSocketNotifier::Read, this);
 | 
			
		||||
    connect(sn_term, SIGNAL(activated(int)), this, SLOT(handleSigTerm()));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
HeadlessManager::~HeadlessManager()
 | 
			
		||||
ServiceManager::~ServiceManager()
 | 
			
		||||
{
 | 
			
		||||
    VitaMTP_Cleanup();
 | 
			
		||||
    delete m_db;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void HeadlessManager::refreshDatabase()
 | 
			
		||||
void ServiceManager::refreshDatabase()
 | 
			
		||||
{
 | 
			
		||||
    if(m_db->load()) {
 | 
			
		||||
        return;
 | 
			
		||||
@@ -67,13 +54,15 @@ void HeadlessManager::refreshDatabase()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void HeadlessManager::start()
 | 
			
		||||
void ServiceManager::start()
 | 
			
		||||
{
 | 
			
		||||
    if(VitaMTP_Init() < 0) {
 | 
			
		||||
        qCritical("Cannot initialize VitaMTP library");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    loadDefaultSettings();
 | 
			
		||||
 | 
			
		||||
    if(QSettings().value("useMemoryStorage", true).toBool()) {
 | 
			
		||||
        m_db = new QListDB();
 | 
			
		||||
    } else {
 | 
			
		||||
@@ -93,9 +82,6 @@ void HeadlessManager::start()
 | 
			
		||||
 | 
			
		||||
    if(!settings.value("disableUSB", false).toBool()) {
 | 
			
		||||
 | 
			
		||||
        if(!belongsToGroup("vitamtp"))
 | 
			
		||||
            qCritical() << tr("This user doesn't belong to the vitamtp group, there could be a problem while reading the USB bus.");
 | 
			
		||||
 | 
			
		||||
        usb_thread = new QThread();
 | 
			
		||||
        client = new CmaClient(m_db);
 | 
			
		||||
        usb_thread->setObjectName("usb_thread");
 | 
			
		||||
@@ -135,19 +121,19 @@ void HeadlessManager::start()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void HeadlessManager::receiveMessage(QString message)
 | 
			
		||||
void ServiceManager::receiveMessage(QString message)
 | 
			
		||||
{
 | 
			
		||||
    QTextStream(stdout) << message << endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void HeadlessManager::stop()
 | 
			
		||||
void ServiceManager::stop()
 | 
			
		||||
{
 | 
			
		||||
    if(CmaClient::stop() < 0) {
 | 
			
		||||
        QCoreApplication::quit();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void HeadlessManager::threadStopped()
 | 
			
		||||
void ServiceManager::threadStopped()
 | 
			
		||||
{
 | 
			
		||||
    mutex.lock();
 | 
			
		||||
    if(--thread_count == 0) {
 | 
			
		||||
@@ -156,36 +142,54 @@ void HeadlessManager::threadStopped()
 | 
			
		||||
    mutex.unlock();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void HeadlessManager::hupSignalHandler(int)
 | 
			
		||||
void ServiceManager::loadDefaultSettings()
 | 
			
		||||
{
 | 
			
		||||
    char a = 1;
 | 
			
		||||
    ::write(sighup_fd[0], &a, sizeof(a));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void HeadlessManager::termSignalHandler(int)
 | 
			
		||||
{
 | 
			
		||||
    char a = 1;
 | 
			
		||||
    ::write(sigterm_fd[0], &a, sizeof(a));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void HeadlessManager::handleSigTerm()
 | 
			
		||||
{
 | 
			
		||||
    sn_term->setEnabled(false);
 | 
			
		||||
    char tmp;
 | 
			
		||||
    ::read(sigterm_fd[1], &tmp, sizeof(tmp));
 | 
			
		||||
 | 
			
		||||
    stop();
 | 
			
		||||
 | 
			
		||||
    sn_term->setEnabled(true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void HeadlessManager::handleSigHup()
 | 
			
		||||
{
 | 
			
		||||
    sn_hup->setEnabled(false);
 | 
			
		||||
    char tmp;
 | 
			
		||||
    ::read(sighup_fd[1], &tmp, sizeof(tmp));
 | 
			
		||||
 | 
			
		||||
    refreshDatabase();
 | 
			
		||||
 | 
			
		||||
    sn_hup->setEnabled(true);
 | 
			
		||||
    QString defaultdir;
 | 
			
		||||
    QSettings settings;
 | 
			
		||||
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
 | 
			
		||||
    qDebug("photoPath: %s", qPrintable(defaultdir));
 | 
			
		||||
    settings.setValue("photoPath", defaultdir);
 | 
			
		||||
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::MusicLocation);
 | 
			
		||||
    qDebug("musicPath: %s", qPrintable(defaultdir));
 | 
			
		||||
    settings.setValue("musicPath", defaultdir);
 | 
			
		||||
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::MoviesLocation);
 | 
			
		||||
    qDebug("photoPath: %s", qPrintable(defaultdir));
 | 
			
		||||
    settings.setValue("videoPath", defaultdir);
 | 
			
		||||
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
 | 
			
		||||
    defaultdir.append(QDir::separator()).append("PS Vita");
 | 
			
		||||
    qDebug("appsPath: %s", qPrintable(defaultdir));
 | 
			
		||||
    settings.setValue("appsPath", defaultdir);
 | 
			
		||||
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
 | 
			
		||||
    defaultdir.append(QDir::separator()).append("PSV Updates");
 | 
			
		||||
    qDebug("urlPath: %s", qPrintable(defaultdir));
 | 
			
		||||
    settings.setValue("urlPath", defaultdir);
 | 
			
		||||
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
 | 
			
		||||
    defaultdir.append(QDir::separator()).append("PSV Packages");
 | 
			
		||||
    qDebug("pkgPath: %s", qPrintable(defaultdir));
 | 
			
		||||
    settings.setValue("pkgPath", defaultdir);
 | 
			
		||||
 | 
			
		||||
    settings.setValue("offlineMode", true);
 | 
			
		||||
    settings.setValue("skipMetadata", false);
 | 
			
		||||
 | 
			
		||||
    // disable USB for now
 | 
			
		||||
    settings.setValue("disableUSB", true);
 | 
			
		||||
 | 
			
		||||
    settings.setValue("disableWireless", false);
 | 
			
		||||
    settings.setValue("useMemoryStorage", true);
 | 
			
		||||
 | 
			
		||||
    settings.setValue("photoSkip", false);
 | 
			
		||||
    settings.setValue("videoSkip", false);
 | 
			
		||||
    settings.setValue("musicSkip", false);
 | 
			
		||||
 | 
			
		||||
    settings.setValue("protocolMode", "automatic");
 | 
			
		||||
 | 
			
		||||
    settings.setValue("protocolIndex", 0);
 | 
			
		||||
 | 
			
		||||
    settings.setValue("protocolVersion", VITAMTP_PROTOCOL_MAX_VERSION);
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user