Removed context from logging.
Fixed homedir detection. Fixed config file creation. Send smartphone device model on broadcast.
This commit is contained in:
		@@ -3,7 +3,7 @@ include(../common/defines.pri)
 | 
			
		||||
 | 
			
		||||
TARGET = qcma_android
 | 
			
		||||
TEMPLATE=app
 | 
			
		||||
QT += network sql
 | 
			
		||||
QT += network sql androidextras
 | 
			
		||||
LIBS += -L../common -lqcma_common
 | 
			
		||||
 | 
			
		||||
# this library needs to link statically their deps but Qt doesn't pass --static to PKGCONFIG
 | 
			
		||||
 
 | 
			
		||||
@@ -21,20 +21,57 @@
 | 
			
		||||
#include <QDebug>
 | 
			
		||||
#include <QLibraryInfo>
 | 
			
		||||
#include <QLocale>
 | 
			
		||||
#include <QFile>
 | 
			
		||||
#include <QStringList>
 | 
			
		||||
#include <QTextCodec>
 | 
			
		||||
#include <QThread>
 | 
			
		||||
#include <QTranslator>
 | 
			
		||||
#include <QStandardPaths>
 | 
			
		||||
 | 
			
		||||
#include <inttypes.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#include <vitamtp.h>
 | 
			
		||||
 | 
			
		||||
#include <android/log.h>
 | 
			
		||||
 | 
			
		||||
#include "servicemanager.h"
 | 
			
		||||
 | 
			
		||||
static QString getAppName()
 | 
			
		||||
{
 | 
			
		||||
    pid_t pid = getpid();
 | 
			
		||||
    QString cmdline = QString("/proc/%1/cmdline").arg(pid);
 | 
			
		||||
    QFile file(cmdline);
 | 
			
		||||
    return file.open(QIODevice::ReadOnly) ? file.readLine() : "";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static void cleanOutput(QtMsgType type, const QMessageLogContext &, const QString & msg)
 | 
			
		||||
{
 | 
			
		||||
    QByteArray localMsg = msg.toLocal8Bit();
 | 
			
		||||
    const char *message = localMsg.constData();
 | 
			
		||||
 | 
			
		||||
    switch (type) {
 | 
			
		||||
    case QtDebugMsg:
 | 
			
		||||
        __android_log_print(ANDROID_LOG_INFO, "qcma", "%s", message);
 | 
			
		||||
        break;
 | 
			
		||||
    case QtWarningMsg:
 | 
			
		||||
        __android_log_print(ANDROID_LOG_WARN, "qcma", "%s", message);
 | 
			
		||||
        break;
 | 
			
		||||
    case QtCriticalMsg:
 | 
			
		||||
        __android_log_print(ANDROID_LOG_ERROR, "qcma", "%s", message);
 | 
			
		||||
        break;
 | 
			
		||||
    case QtFatalMsg:
 | 
			
		||||
        __android_log_print(ANDROID_LOG_FATAL, "qcma", "%s", message);
 | 
			
		||||
        abort();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
    QCoreApplication app(argc, argv);
 | 
			
		||||
    app.setApplicationName("Qcma");
 | 
			
		||||
    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
 | 
			
		||||
@@ -44,8 +81,18 @@ int main(int argc, char *argv[])
 | 
			
		||||
    // stdout goes to /dev/null on android
 | 
			
		||||
    VitaMTP_Set_Logging(VitaMTP_NONE);
 | 
			
		||||
 | 
			
		||||
    qInstallMessageHandler(cleanOutput);
 | 
			
		||||
 | 
			
		||||
    qDebug("Starting Qcma %s", QCMA_VER);
 | 
			
		||||
 | 
			
		||||
    QString appname = getAppName();
 | 
			
		||||
    qDebug("Class name: %s", qPrintable(appname));
 | 
			
		||||
 | 
			
		||||
    // set $HOME, Qt/Android doesn't currently do this
 | 
			
		||||
    qputenv("HOME", QString("/data/data/%1").arg(appname).toLocal8Bit());
 | 
			
		||||
 | 
			
		||||
    app.addLibraryPath(appname + "/lib");
 | 
			
		||||
 | 
			
		||||
    QTranslator translator;
 | 
			
		||||
    QString locale = "en"; //QLocale().system().name();
 | 
			
		||||
    qDebug() << "Current locale:" << locale;
 | 
			
		||||
@@ -71,7 +118,7 @@ int main(int argc, char *argv[])
 | 
			
		||||
    qDebug("Starting main thread: 0x%016" PRIxPTR, (uintptr_t)QThread::currentThreadId());
 | 
			
		||||
 | 
			
		||||
    // set the organization/application for QSettings to work properly
 | 
			
		||||
    app.setOrganizationName("codestation");
 | 
			
		||||
    app.setOrganizationName("qcma");
 | 
			
		||||
    app.setApplicationName("qcma");
 | 
			
		||||
 | 
			
		||||
    ServiceManager manager;
 | 
			
		||||
 
 | 
			
		||||
@@ -28,6 +28,7 @@
 | 
			
		||||
#include <QSettings>
 | 
			
		||||
#include <QStandardPaths>
 | 
			
		||||
#include <QTextStream>
 | 
			
		||||
#include <QAndroidJniObject>
 | 
			
		||||
#include <vitamtp.h>
 | 
			
		||||
 | 
			
		||||
ServiceManager::ServiceManager(QObject *obj_parent) :
 | 
			
		||||
@@ -56,10 +57,7 @@ void ServiceManager::refreshDatabase()
 | 
			
		||||
 | 
			
		||||
void ServiceManager::start()
 | 
			
		||||
{
 | 
			
		||||
    if(VitaMTP_Init() < 0) {
 | 
			
		||||
        qCritical("Cannot initialize VitaMTP library");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    VitaMTP_Init();
 | 
			
		||||
 | 
			
		||||
    loadDefaultSettings();
 | 
			
		||||
 | 
			
		||||
@@ -144,9 +142,19 @@ void ServiceManager::threadStopped()
 | 
			
		||||
 | 
			
		||||
void ServiceManager::loadDefaultSettings()
 | 
			
		||||
{
 | 
			
		||||
    QString defaultdir;
 | 
			
		||||
    QString defaultdir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
 | 
			
		||||
 | 
			
		||||
    // save config to /sdcard, remove when the app works properly
 | 
			
		||||
    qputenv("XDG_CONFIG_HOME", defaultdir.toLocal8Bit());
 | 
			
		||||
 | 
			
		||||
    QSettings settings;
 | 
			
		||||
 | 
			
		||||
    // skip initialization if some config is already present
 | 
			
		||||
    if(!settings.value("appsPath").isNull())
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    qDebug("saving to: %s", qPrintable(settings.fileName()));
 | 
			
		||||
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
 | 
			
		||||
    qDebug("photoPath: %s", qPrintable(defaultdir));
 | 
			
		||||
    settings.setValue("photoPath", defaultdir);
 | 
			
		||||
@@ -159,23 +167,23 @@ void ServiceManager::loadDefaultSettings()
 | 
			
		||||
    qDebug("photoPath: %s", qPrintable(defaultdir));
 | 
			
		||||
    settings.setValue("videoPath", defaultdir);
 | 
			
		||||
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
 | 
			
		||||
    defaultdir.append(QDir::separator()).append("PS Vita");
 | 
			
		||||
    qDebug("appsPath: %s", qPrintable(defaultdir));
 | 
			
		||||
    settings.setValue("appsPath", defaultdir);
 | 
			
		||||
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
 | 
			
		||||
    defaultdir.append(QDir::separator()).append("PSV Updates");
 | 
			
		||||
    qDebug("urlPath: %s", qPrintable(defaultdir));
 | 
			
		||||
    settings.setValue("urlPath", defaultdir);
 | 
			
		||||
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
 | 
			
		||||
    defaultdir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
 | 
			
		||||
    defaultdir.append(QDir::separator()).append("PSV Packages");
 | 
			
		||||
    qDebug("pkgPath: %s", qPrintable(defaultdir));
 | 
			
		||||
    settings.setValue("pkgPath", defaultdir);
 | 
			
		||||
 | 
			
		||||
    settings.setValue("offlineMode", true);
 | 
			
		||||
    settings.setValue("skipMetadata", false);
 | 
			
		||||
    settings.setValue("skipMetadata", true);
 | 
			
		||||
 | 
			
		||||
    // disable USB for now
 | 
			
		||||
    settings.setValue("disableUSB", true);
 | 
			
		||||
@@ -192,4 +200,12 @@ void ServiceManager::loadDefaultSettings()
 | 
			
		||||
    settings.setValue("protocolIndex", 0);
 | 
			
		||||
 | 
			
		||||
    settings.setValue("protocolVersion", VITAMTP_PROTOCOL_MAX_VERSION);
 | 
			
		||||
 | 
			
		||||
    QString deviceName = QAndroidJniObject::getStaticObjectField(
 | 
			
		||||
                "android/os/Build", "MODEL", "Ljava/lang/String;").toString();
 | 
			
		||||
 | 
			
		||||
    qDebug("Detected device model: %s", qPrintable(deviceName));
 | 
			
		||||
    settings.setValue("hostName", deviceName);
 | 
			
		||||
 | 
			
		||||
    settings.sync();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user