Newer
Older
saori / saoriwindow.cpp
@TAM (Teppei Tamra) TAM (Teppei Tamra) on 25 Nov 2018 10 KB 新しいMediaView追加。
/***

The MIT License

Copyright (c) 2018 Teppei Tamra (TAM)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

***/

#include <QMdiSubWindow>
#include <QPixmap>
#include <QLabel>
#include "saoridef.h"
#include "saoriwindow.h"
#include "ui_saoriwindow.h"
#include "saoriapplication.h"
#include "saoriaddaccountdialog.h"
#include "saoriview.h"
#include "saoriaccount.h"
#include "saorimediaview.h"

SaoriWindow::SaoriWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SaoriWindow)
{
    m_geometry = SaoriApplication::saori()->setting()->value(SAORI_SETTING_GEOMETRY,QRect(0,0,1000,700)).toRect();
    ui->setupUi(this);
    connect(ui->action_Quit,&QAction::triggered,SaoriApplication::saori(),&SaoriApplication::quit);
    m_configDialog = new SaoriConfigureDialog();
    m_configDialog->setModal(true);
    ui->mdiArea->setActivationOrder(QMdiArea::ActivationHistoryOrder);
    connect(m_configDialog,&SaoriConfigureDialog::accountsUpdated,this,&SaoriWindow::updateTimelineList);
    m_tray = new SaoriSystemTray();
    connect(m_tray,&SaoriSystemTray::showMainwindow,this,[=](){
        if (isHidden()) {
            show();
            setGeometry(m_geometry);
        }
    });
    updateTimelineList();
    openPinedView();
    setGeometry(m_geometry);
}

SaoriWindow::~SaoriWindow()
{
    m_geometry = geometry();
    delete ui;
    delete m_configDialog;
    SaoriApplication::saori()->setting()->setValue(SAORI_SETTING_GEOMETRY,m_geometry);
}

QPair<QString,QString> SaoriWindow::getTimelineTitle(const QString timeline)
{
    // trick for translation.
    auto t = timeline.split(':');
    QStringList trtl;
    QString tlText,icon;
    trtl << "home" << "local" << "public" << "notifications" << "instance" << "accounts" << "favourites";
    switch (trtl.indexOf(t[0])) {
    case 0:
        tlText = tr("home");
        icon = SAORI_ICONS_CHAT;
        break;
    case 1:
        tlText = tr("local");
        icon = SAORI_ICONS_TEXT;
        break;
    case 2:
        tlText = tr("public");
        icon = SAORI_ICONS_GLOBAL;
        break;
    case 3:
        tlText = tr("notifications");
        icon = SAORI_ICONS_NOTIFICATIONS;
        break;
    case 4:
        tlText = tr("instance");
        icon = SAORI_ICONS_GLOBAL;
        break;
    case 5:
        tlText = tr("accounts");
        icon = SAORI_ICONS_PERSON;
        break;
    case 6:
        tlText = tr("favourites");
        icon = SAORI_ICONS_STAR;
        break;
    default:
        tlText = timeline;
        break;
    }
    if (t.count() > 1)
        for(int i = 1;i < t.count();i ++) {
            if ((i == 2) && (t[0] == "accounts")) {
                QStringList tl;
                tl << "statuses" << "following" << "followers";
                switch (tl.indexOf(t[2])) {
                case 0:
                    tlText += ":" + tr("statuses");
                    break;
                case 1:
                    tlText += ":" + tr("following");
                    break;
                case 2:
                    tlText += ":" + tr("followers");
                    break;
                default:
                    tlText += ":" + t[i];
                    break;
                }
            } else tlText += ":" + t[i];
        }
    return QPair<QString,QString>(tlText,icon);

}

void SaoriWindow::openPinedView()
{
    QList<QStringList> v;
    SaoriApplication::saori()->setting()->beginGroup(SAORI_SETTING_VIEW);
    for (auto a:*SaoriApplication::saori()->getAccountList()) {
        SaoriApplication::saori()->setting()->beginGroup(a->name());
        for (auto g:SaoriApplication::saori()->setting()->childGroups()) {
            if (SaoriApplication::saori()->setting()->value(g + SAORI_SETTING_VIEW_PINED,false).toBool())
                v.append(QStringList() << a->name() << g);
        }
        SaoriApplication::saori()->setting()->endGroup();
    }
    SaoriApplication::saori()->setting()->endGroup();
    for (auto o:v) openView(o);
}

void SaoriWindow::openView(const QStringList viewName)
{
    if (auto view = SaoriView::findView(viewName.at(1),viewName.at(0))) {
        ui->mdiArea->setActiveSubWindow(qobject_cast<QMdiSubWindow*>(view->parent()));
        return;
    }
    QString title = getTimelineTitle(viewName.at(1)).first + ":" + viewName.at(0);
    auto view = new SaoriView(viewName.at(1),viewName.at(0));
    auto sub = ui->mdiArea->addSubWindow(view);
    connect(view,&SaoriView::openMediaView,this,&SaoriWindow::openMediaView);
    connect(view,&SaoriView::openAccountView,this,&SaoriWindow::openView);
    connect(view,&SaoriView::newEntryRecived,this,[=](const QString view,const QString account,int count){
        auto t = getTimelineTitle(view);
        m_tray->showMessage(t.first + '@' + account,count == 1 ? tr("new entry.") : tr("new %1 entries.").arg(count));
    });
    sub->setWindowTitle(title);
    sub->setWindowIcon(QIcon(getTimelineTitle(viewName.at(1)).second));

    auto setting = SaoriApplication::saori()->setting();
    QString c = SAORI_SETTING_VIEW;
    c += "/" + viewName.at(0) + "/" + viewName.at(1);
    QRect defRect((ui->mdiArea->subWindowList().count() - 1) * 24,
                  (ui->mdiArea->subWindowList().count() - 1) * 24,
                  450,
                  600);
    if (setting->value(c + SAORI_SETTING_VIEW_PINED,false).toBool()) {
        sub->setGeometry(setting->value(c + SAORI_SETTING_VIEW_GEOMETRY,defRect).toRect());
    } else {
        sub->setGeometry(defRect);
    }
    sub->show();
}

void SaoriWindow::openMediaView(const QUrl url)
{
    // TODO 専用のwidget作った方がいいでしょう。
    auto view = new SaoriMediaView();
    auto sub = ui->mdiArea->addSubWindow(view);
    // TODO 以下、SaoriMediaWidgetに実装し直す。
    sub->setWindowTitle(url.toString());
    QString image = SaoriApplication::saori()->cache()->fileCache(url);
    if (image.mid(0,2) == ":/")
        connect(SaoriApplication::saori()->cache(),&SaoriCache::downloaded,this,[=](const QUrl i){
            if (i.toString() == url.toString()) {
                view->setImage(QImage(SaoriApplication::saori()->cache()->fileCache(url)));
                //sub->resize(view->pixmap()->size());
            }
        });
    view->setImage(QImage(image));
    view->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    sub->show();
    // TODO 以下とりあえず。
    sub->resize(400,400);
}

void SaoriWindow::closeEvent(QCloseEvent *event)
{
    m_geometry = geometry();
    if (SaoriApplication::saori()->setting()->value(SAORI_SETTING_CLOSEBEHIDE,SAORI_SETTING_CLOSEBEHIDE_DEFAULT).toBool()) {
        hide();
        event->ignore();
        return;
    }
    QMainWindow::closeEvent(event);
}

void SaoriWindow::updateTimelineList()
{
    ui->timelineTree->clear();
    auto *accountItem = new QTreeWidgetItem(ui->timelineTree,QStringList() << tr("Accounts"));
    accountItem->setIcon(0,QIcon(SAORI_ICONS_PERSONS));
    for (int i = 0;i < SaoriApplication::getAccountList()->count();i ++) {
        auto *item = new QTreeWidgetItem(accountItem,QStringList() << SaoriApplication::getAccountList()->at(i)->name());
        item->setIcon(0,QIcon(SAORI_ICONS_PERSON));
        accountItem->addChild(item);
        for (auto tl:SaoriApplication::getAccountList()->at(i)->timelineList()) {
            auto title = getTimelineTitle(tl);
            auto tlItem = new QTreeWidgetItem(item,QStringList() << title.first
                                              << SaoriApplication::getAccountList()->at(i)->name()
                                              << tl);
            if (!title.second.isEmpty()) tlItem->setIcon(0,QIcon(title.second));
            item->addChild(tlItem);
        }
    }
    auto *instanceItem = new QTreeWidgetItem(ui->timelineTree,QStringList() << tr("Instances"));
    instanceItem->setIcon(0,QIcon(SAORI_ICONS_GLOBAL));
    for (int i = 0;i < SaoriApplication::getInstanceList()->count();i ++) {
        auto *item = new QTreeWidgetItem(instanceItem,QStringList()
                                         << SaoriApplication::getInstanceList()->at(i)->instance().toString());
        item->setIcon(0,QIcon(SAORI_ICONS_GLOBAL));
        instanceItem->addChild(item);
    }
}

void SaoriWindow::on_timelineTree_itemDoubleClicked(QTreeWidgetItem *item, int)
{
    auto account = SaoriApplication::findAccount(item->text(1));
    if (account) {
        openView(QStringList() << item->text(1) << item->text(2));
        return;
    }
    auto instance = SaoriApplication::findInstance(QUrl(item->text(0)));
    if (instance) {
        openView(QStringList() << instance->instance().toString() << "instance");
        return;
    }
}

void SaoriWindow::on_actionTabbedView_mode_toggled(bool arg1)
{
    if (ui->mdiArea->viewMode() == QMdiArea::TabbedView) {
        if (arg1 == false) {
            ui->mdiArea->setViewMode(QMdiArea::SubWindowView);
            ui->actionTiled->setEnabled(true);
        }
    } else {
        if (arg1 == true) {
            ui->mdiArea->setViewMode(QMdiArea::TabbedView);
            ui->actionTiled->setEnabled(false);
        }
    }
    return;
}

void SaoriWindow::on_actionConfigure_triggered()
{
    m_configDialog->show();
    m_configDialog->raise();
    m_configDialog->activateWindow();
}