main.cpp 4.84 KB
Newer Older
1
/***************************************************************************
2
 * Copyright (C) 2015-2016 by Savoir-faire Linux                                *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * Author: Edric Ladent Milaret <edric.ladent-milaret@savoirfairelinux.com>*
 *                                                                         *
 * 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 "mainwindow.h"
#include <QApplication>
#include <QFile>

23
#include "callmodel.h"
24 25 26 27
#include "media/audio.h"
#include "media/video.h"
#include "media/text.h"
#include "media/file.h"
28

29 30

#include <QThread>
31 32
#include <QTranslator>
#include <QLibraryInfo>
33

34

35
#ifdef Q_OS_WIN
36
#include <windows.h>
37
#endif
38

39 40 41 42
#ifdef URI_PROTOCOL
#include "shmclient.h"
#endif

43 44
REGISTER_MEDIA();

45 46 47
void
Console()
{
48
#ifdef Q_OS_WIN
49 50 51 52 53 54 55 56 57 58
    AllocConsole();
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);

    COORD coordInfo;
    coordInfo.X = 130;
    coordInfo.Y = 9000;

    SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coordInfo);
    SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE),ENABLE_QUICK_EDIT_MODE| ENABLE_EXTENDED_FLAGS);
59
#endif
60 61
}

62 63 64
int
main(int argc, char *argv[])
{
Edric Milaret's avatar
Edric Milaret committed
65 66
    qputenv("QT_DEVICE_PIXEL_RATIO", "auto");

67 68
    QApplication a(argc, argv);

69
    auto startMinimized = false;
70
    QString uri = "";
71 72 73 74 75 76

    for (auto string : QCoreApplication::arguments()) {
        if (string == "-m" || string == "--minimized")
            startMinimized = true;
        if (string == "-d" || string == "--debug")
            Console();
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        if (string.startsWith("ring:")) {
            uri = string;
        }
    }

#ifdef URI_PROTOCOL
    QSharedMemory* shm = new QSharedMemory("RingShm");
    QSystemSemaphore* sem = new QSystemSemaphore("RingSem", 0);

    if (not shm->create(1024)) {
        if (not uri.isEmpty()) {
            shm->attach();
            shm->lock();
            char *to = (char*) shm->data();
            QChar *data = uri.data();
            while (!data->isNull())
            {
                memset(to, data->toLatin1(), 1);
                ++data;
                ++to;
            }
            memset(to, 0, 1); //null terminator
            shm->unlock();
        }
        sem->release();

        delete shm;
        exit(EXIT_SUCCESS);
105
    }
106 107 108 109
    //Client listening to shm event
    memset((char*)shm->data(), 0, shm->size());
    ShmClient* shmClient = new ShmClient(shm, sem);
#endif
110

111 112 113 114 115 116 117 118 119 120 121 122 123
    QTranslator qtTranslator;
    qtTranslator.load("qt_" + QLocale::system().name(),
                      QLibraryInfo::location(QLibraryInfo::TranslationsPath));
    a.installTranslator(&qtTranslator);

    QTranslator lrcTranslator;
    lrcTranslator.load("share/libringclient/translations/lrc_" + QLocale::system().name());
    a.installTranslator(&lrcTranslator);

    QTranslator mainTranslator;
    mainTranslator.load("share/ring/translations/ring_client_windows_" + QLocale::system().name());
    a.installTranslator(&mainTranslator);

124 125 126 127 128 129 130 131 132 133 134
    QFont font;
    font.setFamily("Segoe UI");
    a.setFont(font);

    QFile file(":/stylesheet.css");
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        a.setStyleSheet(file.readAll());
        file.close();
    }

135 136 137 138
    QCoreApplication::setOrganizationName("Savoir-faire Linux");
    QCoreApplication::setOrganizationDomain("ring.cx");
    QCoreApplication::setApplicationName("Ring");

139 140
    if (not uri.isEmpty()) {
        startMinimized = false;
141
        MainWindow::instance().onRingEvent(uri);
142 143
    }

144
    if (not startMinimized)
145
        MainWindow::instance().show();
146
    else
147
        MainWindow::instance().showMinimized();
148

149
    MainWindow::instance().createThumbBar();
150

151
#ifdef URI_PROTOCOL
152
    QObject::connect(shmClient, SIGNAL(RingEvent(QString)), &MainWindow::instance(), SLOT(onRingEvent(QString)));
153 154 155 156 157 158 159 160 161

    QObject::connect(&a, &QApplication::aboutToQuit, [&a, &shmClient, &shm, &sem]() {
        shmClient->terminate();
        delete shmClient;
        delete shm;
        delete sem;
    });
#endif

162 163
    return a.exec();
}