File indexing completed on 2024-05-19 05:08:16

0001 /*
0002     SPDX-FileCopyrightText: 2000-2002 Michael Edwardes <mte@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2000-2002 Javier Campos Morales <javi_c@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2000-2002 Felix Rodriguez <frodriguez@users.sourceforge.net>
0005     SPDX-FileCopyrightText: 2000-2002 John C <thetacoturtle@users.sourceforge.net>
0006     SPDX-FileCopyrightText: 2000-2002 Kevin Tambascio <ktambascio@users.sourceforge.net>
0007     SPDX-FileCopyrightText: 2017 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0008     SPDX-FileCopyrightText: 2002-2019 Thomas Baumgart <tbaumgart@kde.org>
0009     SPDX-FileCopyrightText: 2021 Dawid Wróbel <me@dawidwrobel.com>
0010     SPDX-License-Identifier: GPL-2.0-or-later
0011 */
0012 
0013 #include "khomeview_p.h"
0014 
0015 // ----------------------------------------------------------------------------
0016 // QT Includes
0017 #include <QPrintPreviewDialog>
0018 #include <QTimer>
0019 
0020 // ----------------------------------------------------------------------------
0021 // KDE Includes
0022 
0023 // ----------------------------------------------------------------------------
0024 // Project Includes
0025 
0026 #include "kmm_printer.h"
0027 
0028 KHomeView::KHomeView(QWidget *parent) :
0029     KMyMoneyViewBase(*new KHomeViewPrivate(this), parent)
0030 {
0031 }
0032 
0033 KHomeView::~KHomeView()
0034 {
0035 }
0036 
0037 void KHomeView::slotAdjustScrollPos()
0038 {
0039     Q_D(KHomeView);
0040 
0041     if (d && d->m_view)
0042         d->m_view->verticalScrollBar()->setValue(d->m_scrollBarPos);
0043 }
0044 
0045 bool KHomeView::eventFilter(QObject* o, QEvent* e)
0046 {
0047     Q_D(KHomeView);
0048     if (o == d->m_view) {
0049         // we simply suppress the context menu as it
0050         // does not provide useful functions
0051         if (e->type() == QEvent::ContextMenu) {
0052             return true;
0053         }
0054     }
0055     return KMyMoneyViewBase::eventFilter(o, e);
0056 }
0057 
0058 void KHomeView::executeAction(eMenu::Action action, const SelectedObjects& selections)
0059 {
0060     Q_UNUSED(selections)
0061 
0062     Q_D(KHomeView);
0063     switch (action) {
0064     case eMenu::Action::FileNew:
0065         d->m_fileOpen = true;
0066         break;
0067     case eMenu::Action::Print:
0068         if (d->isActiveView()) {
0069             slotPrintView();
0070         }
0071         break;
0072     case eMenu::Action::PrintPreview:
0073         if (d->isActiveView()) {
0074             slotPrintPreviewView();
0075         }
0076         break;
0077     case eMenu::Action::FileClose:
0078         d->m_fileOpen = false;
0079         d->loadView();
0080         break;
0081     default:
0082         break;
0083     }
0084 }
0085 
0086 void KHomeView::executeCustomAction(eView::Action action)
0087 {
0088     switch(action) {
0089     case eView::Action::Refresh:
0090         refresh();
0091         break;
0092 
0093     default:
0094         break;
0095     }
0096 }
0097 
0098 void KHomeView::refresh()
0099 {
0100     Q_D(KHomeView);
0101     if (isVisible() && !d->m_skipRefresh && !d->m_resizeRefreshTimer.isActive()) {
0102         d->loadView();
0103         d->m_needsRefresh = false;
0104     } else {
0105         d->m_needsRefresh = true;
0106     }
0107 }
0108 
0109 void KHomeView::resizeEvent(QResizeEvent* event)
0110 {
0111     Q_D(KHomeView);
0112     d->repaintAfterResize(event->oldSize(), event->size());
0113     KMyMoneyViewBase::resizeEvent(event);
0114 }
0115 
0116 void KHomeView::showEvent(QShowEvent* event)
0117 {
0118     Q_D(KHomeView);
0119     if (d->m_needLoad)
0120         d->init();
0121 
0122     if (d->m_needsRefresh)
0123         refresh();
0124 
0125     QWidget::showEvent(event);
0126 }
0127 
0128 void KHomeView::slotPrintView()
0129 {
0130     Q_D(KHomeView);
0131     if (d->m_view) {
0132         auto printer = KMyMoneyPrinter::startPrint();
0133         if (printer != nullptr) {
0134             d->m_view->print(printer);
0135         }
0136     }
0137 }
0138 
0139 void KHomeView::slotPrintPreviewView()
0140 {
0141     Q_D(KHomeView);
0142     if (d->m_view) {
0143         QPrintPreviewDialog dlg(KMyMoneyPrinter::instance(), d->m_view);
0144         connect(&dlg, &QPrintPreviewDialog::paintRequested, d->m_view, [&](QPrinter* printer) {
0145             Q_D(KHomeView);
0146             d->m_view->print(printer);
0147         });
0148         dlg.exec();
0149     }
0150 }
0151 
0152 void KHomeView::slotOpenUrl(const QUrl &url)
0153 {
0154     Q_D(KHomeView);
0155 
0156     auto triggerAction = [&](eMenu::Action action, const QString& id) {
0157         pActions[action]->setData(id);
0158         Q_EMIT requestActionTrigger(action);
0159     };
0160 
0161     QString protocol = url.scheme();
0162     QString view = url.fileName();
0163 
0164     QUrlQuery query(url);
0165     QString id = query.queryItemValue("id");
0166     QString mode = query.queryItemValue("mode");
0167 
0168     if (protocol == QLatin1String("https")) {
0169         QDesktopServices::openUrl(url);
0170     } else if (protocol == QLatin1String("mailto")) {
0171         QDesktopServices::openUrl(url);
0172     } else {
0173         // empty view -> bail out
0174         if (view.isEmpty())
0175             return;
0176 
0177         KXmlGuiWindow* mw = KMyMoneyUtils::mainWindow();
0178         Q_CHECK_PTR(mw);
0179         if (view == VIEW_LEDGER) {
0180             pActions[eMenu::Action::GoToAccount]->setData(id);
0181             Q_EMIT requestActionTrigger(eMenu::Action::GoToAccount);
0182 
0183         } else if (view == VIEW_SCHEDULE) {
0184             if (mode == QLatin1String("enter")) {
0185                 triggerAction(eMenu::Action::EnterSchedule, id);
0186 
0187             } else if (mode == QLatin1String("edit")) {
0188                 triggerAction(eMenu::Action::EditSchedule, id);
0189 
0190             } else if (mode == QLatin1String("skip")) {
0191                 triggerAction(eMenu::Action::SkipSchedule, id);
0192 
0193             } else if (mode == QLatin1String("full")) {
0194                 d->m_showAllSchedules = true;
0195                 d->loadView();
0196 
0197             } else if (mode == QLatin1String("reduced")) {
0198                 d->m_showAllSchedules = false;
0199                 d->loadView();
0200             }
0201 
0202         } else if (view == VIEW_REPORTS) {
0203             triggerAction(eMenu::Action::ReportOpen, id);
0204 
0205         } else if (view == VIEW_WELCOME) {
0206             d->m_view->setHtml(KWelcomePage::welcomePage());
0207 
0208         } else if (view == QLatin1String("action")) {
0209             QMetaObject::invokeMethod(mw->actionCollection()->action(id), "trigger");
0210 
0211         } else if (view == VIEW_HOME) {
0212             QList<MyMoneyAccount> list;
0213             // it could be, that we don't even have a storage object attached.
0214             // in this case the call to accountList() will throw an MyMoneyException
0215             // which we catch here and treat it as 'no accounts found'.
0216             try {
0217                 MyMoneyFile::instance()->accountList(list);
0218             } catch (const MyMoneyException&) {
0219             }
0220             if (list.count() == 0) {
0221                 KMessageBox::information(this, i18n("Before KMyMoney can give you detailed information about your financial status, you need to create at least one account. Until then, KMyMoney shows the welcome page instead."));
0222             }
0223             d->loadView();
0224 
0225         } else {
0226             qDebug("Unknown view '%s' in slotOpenURL()", qPrintable(view));
0227         }
0228     }
0229 }
0230 
0231 void KHomeView::slotSettingsChanged()
0232 {
0233     refresh();
0234 }
0235 
0236 void KHomeView::slotDisableRefresh()
0237 {
0238     Q_D(KHomeView);
0239     d->m_skipRefresh = true;
0240 }
0241 
0242 void KHomeView::slotEnableRefresh()
0243 {
0244     Q_D(KHomeView);
0245     d->m_skipRefresh = false;
0246     refresh();
0247 }
0248 
0249 // Make sure, that these definitions are only used within this file
0250 // this does not seem to be necessary, but when building RPMs the
0251 // build option 'final' is used and all CPP files are concatenated.
0252 // So it could well be, that in another CPP file these definitions
0253 // are also used.
0254 #undef VIEW_LEDGER
0255 #undef VIEW_SCHEDULE
0256 #undef VIEW_WELCOME
0257 #undef VIEW_HOME
0258 #undef VIEW_REPORTS