~ubuntu-app-review-contributors/ubuntu-app-reviews/jpiiirc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**************************************************************************
Filename: mainwindow.cpp
Author: Juhapekka Piiroinen
Email: juhapekka@jpii.fi
-------------------------------------------------------------------------

Copyright (c) 2012, Juhapekka Piiroinen. All rights reserved.

This file is part of jpiiIRC.

jpiiIRC is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

jpiiIRC 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with jpiiIRC; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301  USA
**************************************************************************/

#include "mainwindow.h"
#include "ui_mainwindow.h"

#ifdef INDICATEQT_ENABLED
#include <qindicateserver.h>
#include <qindicateindicator.h>
#endif

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_tabBar(NULL),
    m_pMenuFile(NULL),
    m_pMenuSettings(NULL),
    m_pMenuPlugins(new QMenu("&Plugins")),
    m_pMenuAbout(new QMenu("&About"))
{
    DIN;
    ui->setupUi(this);
    this->statusBar()->hide();
   // setUnifiedTitleAndToolBarOnMac(true);

    this->setWindowState(Qt::WindowMaximized);

    //this->setWindowOpacity(0.5);
   // QPixmap pm = QPixmap(":/mainwindow/mainwindow.png");
    //setPixmap(pm);
    //setMask(pm.mask());
    //setAttribute(Qt::WA_TranslucentBackground ,true );

    //this->menuBar()->addMenu(m_pMenuPlugins);
    m_pMenuAbout->addAction("Qt..",this,SLOT(showAboutQt()));
    //this->menuBar()->addMenu(m_pMenuAbout);
    ui->mdiArea->setDocumentMode(true);
    QList<QTabBar *> tabBarList = ui->mdiArea->findChildren<QTabBar*>();
    m_tabBar = tabBarList.at(0);
    m_tabBar->setFocusPolicy(Qt::NoFocus);
    //m_tabBar->setShape(QTabBar::RoundedSouth);

    connect(ui->mdiArea,SIGNAL(subWindowActivated(QMdiSubWindow*)),this,SLOT(onSubWindowActivated(QMdiSubWindow*)));
    //m_3dview->setAutoFillBackground(false);

    //Qt::WindowFlags flags = subWindow->windowFlags();
    //flags |= Qt::FramelessWindowHint;
    //flags |= Qt::SplashScreen;
   // subWindow->setWindowFlags(flags);
    //subWindow->setAttribute(Qt::WA_TranslucentBackground);
    loadPlugins();
    DOUT;
}

MainWindow::~MainWindow()
{
    DIN;
    qDebug() << "Removing toolbars:";
    foreach (QString toolBarName, m_loadedToolBars.keys()) {
        qDebug() << toolBarName;
        this->removeToolBar(m_loadedToolBars[toolBarName]);
        m_loadedToolBars[toolBarName]->deleteLater();
        m_loadedToolBars.remove(toolBarName);
    }
    foreach (QAction* action, this->menuBar()->actions()) {
        this->menuBar()->removeAction(action);
        action->deleteLater();
    }

    // TODO: delete all loaded stuff, call unload methods of loaded plugins..

    delete ui;
    DOUT;
}


void MainWindow::changeEvent(QEvent *e) {
    DIN << e << this->isVisible();
/*
    if (e->type()==QEvent::WindowStateChange) {
        qDebug() << "window state Event::WindowStateChange" << this->windowState() << e->spontaneous();
        if (this->windowState() && Qt::WindowMaximized) {
            Qt::WindowFlags flags = this->windowFlags();
            qDebug() << "maximized" << this->windowFlags();
            if (!windowFlags().testFlag(Qt::FramelessWindowHint))  {
                flags |= Qt::FramelessWindowHint;
                setWindowFlags(flags);
                show();
            }

            //this->show();
        }  else {
            qDebug() << "other" << this->windowFlags();
            Qt::WindowFlags flags = windowFlags();
            if (flags.testFlag(Qt::FramelessWindowHint)) {
                flags &= ~Qt::FramelessWindowHint;
                setWindowFlags(flags);
                show();
            }
            //this->show();
        }


    }*/
    DOUT;
    QMainWindow::changeEvent(e);
}


void MainWindow::onSubWindowActivated(QMdiSubWindow* window) {
    if (!window) { return; }
    // TODO: There is something weird on Windows platform on the subWindowActivated when you minimize the mainwindow
    DIN << "a" << window;
    // TODO: this line wont be processed anymore when it happens?!
    qDebug() << "WINDOW TITLE is" << window->windowTitle();
        setTabTextColor(window->windowTitle(),Qt::black);

        iPendingMessages=0;
#ifdef Q_OS_LINUX
    QDBusMessage signal = QDBusMessage::createSignal("/","com.canonical.Unity.LauncherEntry","Update");
    signal << "application://jpiiirc.desktop";
    QVariantMap setProperty;
    setProperty.insert("count",qint64(0));
    setProperty.insert("count-visible",false);
    signal << setProperty;
    QDBusConnection::sessionBus().send(signal);
#endif
    DOUT;
}


void MainWindow::showEvent ( QShowEvent * event ) {
    Q_UNUSED(event);

    DIN;
    if (ui->mdiArea->activeSubWindow()) {
        setTabTextColor(ui->mdiArea->activeSubWindow()->windowTitle(),Qt::black);
    }
    DOUT;
}

void MainWindow::hideEvent ( QHideEvent * event ) {
    Q_UNUSED(event);
    DIN;

    DOUT;
}

void MainWindow::setTabTextColor(QString fulltitle, QColor color) {
    DIN;
    QString title = fulltitle.split(" ").takeFirst();
    qDebug() << m_tabBar->count();
    if (m_tabBar->count()==1) {
        m_tabBar->setTabTextColor(0,Qt::black);
        return;
    }
    for (int idx = 0; idx < m_tabBar->count(); idx++) {
        QString fullTabText = m_tabBar->tabText(idx);
        if (fullTabText.split(" ").takeFirst()==title) {
            qDebug() << ui->mdiArea->activeSubWindow();
            if (ui->mdiArea->activeSubWindow() && ui->mdiArea->activeSubWindow()->windowTitle()==title) {
                // TODO: we do not change the color when it is active window, NOT A CORRECT PLACE
                m_tabBar->setTabTextColor(idx,Qt::black);
                m_tabBar->setTabText(idx,QString("%0").arg(fullTabText.split(" ").takeFirst()));
            } else {
                m_tabBar->setTabTextColor(idx,color);
                iPendingMessages++;
                m_tabBar->setTabText(idx,QString("%0 (%1)").arg(fullTabText.split(" ").takeFirst()).arg(iPendingMessages));

#ifdef Q_OS_LINUX
    QDBusMessage signal = QDBusMessage::createSignal("/","com.canonical.Unity.LauncherEntry","Update");
    signal << "application://jpiiirc.desktop";
    QVariantMap setProperty;
    setProperty.insert("count",qint64(iPendingMessages));
    setProperty.insert("count-visible",true);
    signal << setProperty;
    QDBusConnection::sessionBus().send(signal);
#endif
            }
        }
    }
    DOUT;
}

void MainWindow::unloadPlugin() {
    DIN;
    QAction* action = qobject_cast<QAction*>(sender());
    if (action) {
        unloadPlugin(action->text());
    }
    DOUT;
}

void MainWindow::unloadPlugin(QString fileName) {
    DIN << fileName;
   /* if (m_plugins[fileName]) {
        //m_pluginsInfo[fileName]->unloadInstance();
        qDebug() << "instance unloaded";
        if (m_loadedToolBars.contains(fileName)) {
            this->removeToolBar(m_loadedToolBars[fileName]);
        }
        if (m_loadedDockWidgets.contains(fileName)) {
            this->removeDockWidget(m_loadedDockWidgets[fileName]);
        }
        if (m_loadedPlugins.contains(fileName)) {
            QObject* loadedPlugin = m_loadedPlugins.take(fileName);
            delete loadedPlugin; loadedPlugin = NULL;
        }
        QPluginLoader* loader = m_plugins.take(fileName);
        qDebug() << loader;
        bool ok = loader->unload();
        loader->deleteLater();
        if (ok) {
            //m_plugins[fileName] = NULL;
        } else {
            qWarning() << "could not unload plugin";
        }
    }*/
    /// TODO: not a good idea to do a runtime unloading..
    DOUT;
}

void MainWindow::loadPlugin(QString fileName) {
    DIN;

        QPluginLoader* loader = new QPluginLoader(fileName);
        bool loaded = loader->load();
        if (loaded==false) {
            qWarning() << "Could not load plugin from" << fileName;
            return;
        }
        QObject *plugin = loader->instance();
        if (plugin) {

            qDebug() << "Loaded plugin from" << fileName;

            PluginInformationInterface* iPluginInformation = qobject_cast<PluginInformationInterface*>(plugin);
            if (iPluginInformation) {
                m_pluginsInfo[fileName] = iPluginInformation;
                qDebug() << "Name:" << iPluginInformation->name();
                qDebug() << "Author:" << iPluginInformation->author();
            } else {
                qWarning() << "Could not load plugin information, cancelling";
                loader->unload();
                return;
            }

            ToolBarInterface *iToolBar = qobject_cast<ToolBarInterface *>(plugin);
            if (iToolBar) {
                qDebug() << " * Found toolbar";
                QToolBar* toolBar = iToolBar->toolBarInstance();
                if (toolBar) {
                    m_loadedToolBars.insert(fileName,toolBar);
                    this->addToolBar(toolBar);
                } else {
                    qWarning() << "could not load toolBarInstance";
                   // QMessageBox::warning(this,"Plugin is broken, could not load toolbar",fileName,QMessageBox::Ok);
                }
            }

            DockWidgetInterface *iDockWidget = qobject_cast<DockWidgetInterface *>(plugin);
            if (iDockWidget) {
                qDebug() << " * Found dock widget";
                QDockWidget* dockWidget = iDockWidget->dockWidgetInstance();

                if (dockWidget) {
                    m_loadedDockWidgets.insert(fileName,dockWidget);
                    this->addDockWidget(iDockWidget->widgetArea(),dockWidget);
                } else {
                    qWarning() << "Could not load dockWidgetInstance";
                   //QMessageBox::warning(this,"Plugin is broken, could not load dockwidget",fileName,QMessageBox::Ok);
                }
            }

            MenuInterface *iMenu = qobject_cast<MenuInterface *>(plugin);
            if (iMenu) {
                qDebug() << " * Found menu";
                MenuInterface::MenuType menuType = iMenu->menuType();
                QMenu* menu = iMenu->menuInstance();
                if (menu) {
                    switch (menuType) {
                        case MenuInterface::MENU_ABOUT:
                            if (m_pMenuAbout==NULL) { m_pMenuAbout = new QMenu("&About"); this->menuBar()->addMenu(m_pMenuAbout); }
                            m_pMenuAbout->addMenu(menu);
                            break;
                        case MenuInterface::MENU_FILE:
                            if (m_pMenuFile==NULL) { m_pMenuFile = new QMenu("&File"); this->menuBar()->addMenu(m_pMenuFile); }
                            m_pMenuFile->addMenu(menu);
                            break;
                        case MenuInterface::MENU_PLUGINS:
                            if (m_pMenuPlugins==NULL) { m_pMenuPlugins = new QMenu("&Plugins"); this->menuBar()->addMenu(m_pMenuPlugins); }
                            m_pMenuPlugins->addMenu(menu);
                            break;
                        case MenuInterface::MENU_SETTINGS:
                            if (m_pMenuSettings==NULL) { m_pMenuSettings = new QMenu("&Settings"); this->menuBar()->addMenu(m_pMenuSettings); }
                            m_pMenuSettings->addMenu(menu);
                            break;
                        case MenuInterface::MENU_TOPLEVEL:
                        default:
                            this->menuBar()->addMenu(menu);
                            break;
                    }
                } else {
                    qWarning() << "Could not load menuInstance";
                    //QMessageBox::warning(this,"Plugin is broken, Could not load menu",fileName,QMessageBox::Ok);
                }
            }

            MDIChildInterface *iMdiChild = qobject_cast<MDIChildInterface *>(plugin);
            if (iMdiChild) {
                qDebug() << " * Found mdi child";
                iMdiChild->setParentMdiArea(ui->mdiArea);
                bool ok = connect(plugin,SIGNAL(setTabTextColor(QString,QColor)),this,SLOT(setTabTextColor(QString, QColor)));
                if (!ok) {
                    qWarning() << "Could not connect setTabTextColor";
                    //QMessageBox::warning(this,"Plugin is broken",fileName,QMessageBox::Ok);
                }
            }

            StatusBarInterface *iStatusBar = qobject_cast<StatusBarInterface *>(plugin);
            if (iStatusBar) {
                qDebug() << " * Found statusbar widget";
                QWidget* widget = iStatusBar->statusBarWidgetInstance();
                if (widget) {
                    ui->statusBar->addWidget(widget);
                    widget->show();
                } else {
                    qWarning() << "invalid widget returned, statusbar not loaded";
                  //  QMessageBox::warning(this,"Plugin is broken, could not load statusbar",fileName,QMessageBox::Ok);
                }
            }

            PluginLoaderInterface *iPluginLoader = qobject_cast<PluginLoaderInterface *>(plugin);
            if (iPluginLoader) {
                qDebug() << " * Found PluginLoader";
                bool ok = connect(plugin,SIGNAL(loadPlugin(QString)),this,SLOT(loadPlugin(QString)));
                if (!ok) {
                    qWarning() << "Could not connect loadPlugin signal";
                }
                ok = connect(plugin,SIGNAL(unloadPlugin(QString)),this,SLOT(unloadPlugin(QString)));
                if (!ok) {
                    qWarning() << "Could not connect unloadPlugin signal";
                }
            }

            m_plugins[fileName] = loader;
            m_loadedPlugins[fileName] = plugin;
            m_pMenuPlugins->addAction(fileName,this,SLOT(unloadPlugin()));
        } else {
            m_plugins[fileName] = NULL;
            QMessageBox::warning(this,"Failed to load plugin " + fileName,loader->errorString(),QMessageBox::Ignore);
            plugin->deleteLater();

            loader->unload();
        }

    DOUT;
}


void MainWindow::loadPlugins() {
    DIN;
    QDir pluginsDir = QDir(qApp->applicationDirPath() + "/plugins");
    qDebug() << pluginsDir.absolutePath();
    foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
        if (!fileName.endsWith(".dll") && !fileName.endsWith(".so") && !fileName.endsWith(".dylib")) continue;

        loadPlugin(pluginsDir.absoluteFilePath(fileName));

    }
    DOUT;
}