mainwindow.cpp 6.65 KB
Newer Older
1
/***************************************************************************
2
 * Copyright (C) 2015-2017 by Savoir-faire Linux                           *
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "ui_mainwindow.h"

22
#include <QDesktopWidget>
23 24 25

#include "media/text.h"
#include "media/textrecording.h"
26

27
#ifdef Q_OS_WIN
28 29 30 31 32
#include <windows.h>
#include <QWinThumbnailToolBar>
#include <QWinThumbnailToolButton>
#endif

33
#include "aboutdialog.h"
Edric Milaret's avatar
Edric Milaret committed
34
#include "settingskey.h"
35
#include "winsparkle.h"
36 37
#include "callmodel.h"

38
MainWindow::MainWindow(QWidget* parent) :
39
    QMainWindow(parent),
40
    ui(new Ui::MainWindow)
41 42 43
{
    ui->setupUi(this);

44
    QIcon icon(":images/ring.png");
45 46 47

    this->setWindowIcon(icon);

48 49 50
    GlobalSystemTray& sysIcon = GlobalSystemTray::instance();
    sysIcon.setIcon(icon);

51
    QMenu* menu = new QMenu();
52

53
    auto configAction = new QAction(tr("Configuration"), this);
54 55
    menu->addAction(configAction);

56 57 58 59 60 61
    auto exitAction = new QAction(tr("Exit"), this);
    connect(exitAction, &QAction::triggered, [this](){
        QCoreApplication::exit();
    });
    menu->addAction(exitAction);

62 63 64 65
    sysIcon.setContextMenu(menu);
    sysIcon.show();

    connect(&sysIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
66 67
            this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));

Edric Milaret's avatar
Edric Milaret committed
68
    connect(&CallModel::instance(), SIGNAL(incomingCall(Call*)),
69 70
            this, SLOT(onIncomingCall(Call*)));

71
    navStack_ = new NavStack(ui->stackedWidgetView, this);
72 73 74
    connect(configAction, &QAction::triggered, [this]() {
        navStack_->onNavigationRequested(ScreenEnum::ConfScreen);
    });
75

76
#ifdef Q_OS_WIN
77 78 79 80
    HMENU sysMenu = ::GetSystemMenu((HWND) winId(), FALSE);
    if (sysMenu != NULL) {
        ::AppendMenuA(sysMenu, MF_SEPARATOR, 0, 0);
        QString aboutTitle = tr("About");
81
        ::AppendMenuW(sysMenu, MF_STRING, IDM_ABOUTBOX, aboutTitle.toStdWString().c_str());
82 83
    }
#endif
84

85 86
    QSettings settings;
    QVariant size = settings.value(SettingsKey::savedSize);
87 88
    QVariant posV = settings.value(SettingsKey::savedPos);
    if (size.isValid() && posV.isValid()) {
89
        resize(size.toSize());
90 91 92 93
        auto screenSize = QApplication::desktop()->screenGeometry();
        auto pos = posV.toPoint();
        if (pos.rx() < screenSize.width() && pos.ry() < screenSize.height())
            move(pos);
94 95
    } else
        resize(1054, 600);
96

97
    win_sparkle_set_appcast_url("http://dl.ring.cx/windows/winsparkle-ring.xml");
98 99
    win_sparkle_set_app_details(L"Savoir-faire Linux", L"Ring", QString(NIGHTLY_VERSION).toStdWString().c_str());
    win_sparkle_set_shutdown_request_callback([]() {QCoreApplication::exit();});
100
    win_sparkle_set_did_find_update_callback([]() {MainWindow::instance().showNormal();});
101 102 103 104 105 106 107 108 109
    win_sparkle_init();

    if (win_sparkle_get_last_check_time() == -1) {
        win_sparkle_set_update_check_interval(7 * 86400);
    }

    QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit, [=]() {
        win_sparkle_cleanup();
    });
110 111

    setContextMenuPolicy(Qt::NoContextMenu);
112 113 114 115 116

    if (not settings.contains(SettingsKey::enableNotifications)) {
        settings.setValue(SettingsKey::enableNotifications, true);
    }
    connect(&GlobalSystemTray::instance(), SIGNAL(messageClicked()), this, SLOT(notificationClicked()));
117 118 119 120 121 122

    connect(&netManager_, &QNetworkConfigurationManager::onlineStateChanged, [=](bool online) {
        Q_UNUSED(online)
        AccountModel::instance().slotConnectivityChanged();
    });

123 124 125 126 127
}

MainWindow::~MainWindow()
{
    delete ui;
Edric Milaret's avatar
Edric Milaret committed
128
    delete navStack_;
129 130
}

131
void
132
MainWindow::onRingEvent(const QString& uri)
133 134 135 136 137 138 139 140 141
{
    this->showNormal();
    if (not uri.isEmpty()) {
        auto outCall = CallModel::instance().dialingCall();
        outCall->setDialNumber(uri);
        outCall->performAction(Call::Action::ACCEPT);
    }
}

142
bool
143
MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result)
144
{
145
    Q_UNUSED(eventType)
146

147 148
#ifdef Q_OS_WIN
    MSG* msg = (MSG*) message;
149 150 151 152 153 154 155 156 157 158

    if (msg->message == WM_SYSCOMMAND) {
        if ((msg->wParam & 0xfff0) == IDM_ABOUTBOX) {
            *result = 0;

            AboutDialog aboutDialog;
            aboutDialog.exec();

            return true;
        }
159
    }
160 161 162
#else
    Q_UNUSED(message)
    Q_UNUSED(result)
163 164
#endif
    return false;
165 166 167
}

void
168 169
MainWindow::trayActivated(QSystemTrayIcon::ActivationReason reason)
{
170 171 172
    if (reason != QSystemTrayIcon::ActivationReason::Context)
        this->show();
}
173

174 175 176 177 178
void
MainWindow::notificationClicked() {
    showNormal();
}

179
void
180
MainWindow::onIncomingCall(Call* call)
181
{
182 183 184
    Q_UNUSED(call);
    QWidget::showNormal();
}
185 186 187 188

void
MainWindow::createThumbBar()
{
189 190
#ifdef Q_OS_WIN
    QWinThumbnailToolBar* thumbbar = new QWinThumbnailToolBar(this);
191
    thumbbar->setWindow(this->windowHandle());
192
    QWinThumbnailToolButton* settings = new QWinThumbnailToolButton(thumbbar);
193 194 195 196 197 198 199 200 201
    settings->setToolTip("Settings");
    QIcon icon(":/images/settings.png");
    settings->setIcon(icon);
    settings->setDismissOnClick(true);
    connect(settings, &QWinThumbnailToolButton::clicked, [this]() {
        navStack_->onNavigationRequested(ScreenEnum::ConfScreen);
    });

    thumbbar->addButton(settings);
202
#endif
203
}
204 205 206 207 208 209 210 211 212

void
MainWindow::switchNormalMaximize()
{
    if(isMaximized())
        showNormal();
    else
        showMaximized();
}
Edric Milaret's avatar
Edric Milaret committed
213 214 215 216 217 218 219 220

void
MainWindow::closeEvent(QCloseEvent* event)
{
    QSettings settings;
    if (settings.value(SettingsKey::closeOrMinimized).toBool()) {
        this->hide();
        event->ignore();
221 222 223
    } else {
        settings.setValue(SettingsKey::savedSize, size());
        settings.setValue(SettingsKey::savedPos, pos());
Edric Milaret's avatar
Edric Milaret committed
224 225
    }
}