File indexing completed on 2024-05-19 12:55:03

0001 /**  This file is part of the KDE project
0002  *
0003  *  Copyright (C) 2011 Adam Pigg <adam@piggz.co.uk>
0004  *
0005  *  This library is free software; you can redistribute it and/or
0006  *  modify it under the terms of the GNU Library General Public
0007  *  License as published by the Free Software Foundation; either
0008  *  version 2 of the License, or (at your option) any later version.
0009  *
0010  *  This library is distributed in the hope that it will be useful,
0011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  *  Library General Public License for more details.
0014  *
0015  *  You should have received a copy of the GNU Library General Public License
0016  *  along with this library; see the file COPYING.LIB.  If not, write to
0017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  *  Boston, MA 02110-1301, USA.
0019  */
0020 
0021 #include "KexiMobileMainWindow.h"
0022 #include "KexiMobileWidget.h"
0023 #include "KexiMobileToolbar.h"
0024 #include "KexiMobileNavigator.h"
0025 #include <KexiIcon.h>
0026 #include <kexipart.h>
0027 #include <kexipartinfo.h>
0028 #include <KexiWindow.h>
0029 #include <KexiView.h>
0030 #include <core/KexiRecordNavigatorHandler.h>
0031 
0032 #include <KDbDriverManager>
0033 
0034 #include <KToolBar>
0035 
0036 #include <QMenu>
0037 #include <QMenuBar>
0038 #include <QAction>
0039 #include <QHBoxLayout>
0040 #include <QMimeDatabase>
0041 #include <QMimeType>
0042 #include <QDebug>
0043 #include <QFileDialog>
0044 
0045 KexiMobileMainWindow::KexiMobileMainWindow()
0046 {
0047     m_mobile = new KexiMobileWidget(0);
0048     m_toolbar = new KexiMobileToolbar(this);
0049     m_layout = new QHBoxLayout(this);
0050 
0051     //! part info has to be collected at this stage
0052     KexiPart::PartInfoList *partInfoList = Kexi::partManager().infoList();
0053     Q_UNUSED(partInfoList)
0054 
0055     m_openFileAction = new QAction(koIcon("document-open"), "Open", this);
0056     connect(m_openFileAction, SIGNAL(triggered(bool)), this, SLOT(slotOpenDatabase()));
0057     menuBar()->addAction(m_openFileAction);
0058 
0059     m_layout->addWidget(m_mobile);
0060     m_layout->setSpacing(2);
0061     setFixedSize(800,480);
0062 
0063     setCentralWidget(m_mobile);
0064 
0065     addToolBar(Qt::BottomToolBarArea, m_toolbar);
0066 
0067     connect(m_toolbar, SIGNAL(pageNavigator()), m_mobile, SLOT(showNavigator()));
0068     connect(m_mobile->navigator(), SIGNAL(openItem(KexiPart::Item*)), this, SLOT(openObject(KexiPart::Item*)));
0069 }
0070 
0071 void KexiMobileMainWindow::setupToolbar()
0072 {
0073 
0074 }
0075 
0076 KexiMobileMainWindow::~KexiMobileMainWindow()
0077 {}
0078 
0079 void KexiMobileMainWindow::slotOpenDatabase()
0080 {
0081     QString fileName;
0082 
0083     fileName = QFileDialog::getOpenFileName(this, xi18n("Open Database"), "", xi18n("Database Files (*.kexi)"));
0084 
0085     if (!fileName.isNull()) {
0086         KexiProject *proj = openProject(QUrl::fromLocalFile(fileName));
0087         if (proj) {
0088             m_project = proj;
0089             m_mobile->databaseOpened(proj);
0090         } else {
0091             qWarning() << "Project not returned";
0092         }
0093     }
0094 }
0095 
0096 KexiProject *KexiMobileMainWindow::openProject(const QUrl &url)
0097 {
0098     KDbDriverManager driverManager;
0099     KDbDriver *driver = 0;
0100 
0101     QMimeDatabase db;
0102     QMimeType mime = db.mimeTypeForUrl(url);
0103 
0104     QString driverName = driverManager.lookupByMime(mime.name());
0105     driver = driverManager.driver(driverName.toLower());
0106 
0107     //qDebug() << driverManager.driverNames();
0108     //qDebug() << driverName;
0109 
0110     KexiProjectData *project_data = new KexiProjectData;
0111     project_data->setDatabaseName(url.path());
0112 
0113     //qDebug() << driver;
0114 
0115     if (driver && driver->isFileDriver()) {
0116         project_data->connectionData()->setFileName(url.path());
0117     }
0118 
0119     project_data->connectionData()->driverName = driverName;
0120     KexiProject *project = new KexiProject(*project_data);
0121     return project;
0122 }
0123 
0124 KexiWindow* KexiMobileMainWindow::openObject(KexiPart::Item* item)
0125 {
0126     bool cancelled;
0127     KexiWindow* win = openObject(item, Kexi::DataViewMode, &cancelled);
0128 
0129     if (!cancelled)
0130         return win;
0131 
0132     return 0;
0133 }
0134 
0135 
0136 KexiWindow *
0137 KexiMobileMainWindow::openObject(KexiPart::Item* item, Kexi::ViewMode viewMode, bool *openingCancelled,
0138                                  QMap<QString, QVariant>* staticObjectArgs, QString* errorMessage)
0139 {
0140     Q_ASSERT(openingCancelled);
0141     KexiWindow *window = 0;
0142 
0143     if (!openingAllowed(item, viewMode, errorMessage)) {
0144         if (errorMessage)
0145             *errorMessage = xi18nc(
0146                                 "@info opening is not allowed in \"data view/design view/text view\" mode",
0147                                 "opening is not allowed in <resource>%1</resource> mode",
0148                                 Kexi::nameForViewMode(viewMode));
0149         *openingCancelled = true;
0150         return 0;
0151     }
0152     //qDebug() << m_project << item;
0153 
0154     if (!m_project || !item)
0155         return 0;
0156 
0157 //!TODO Move this to KexiUtils::WaitCursor
0158 #ifdef Q_WS_MAEMO_5
0159     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
0160 #endif
0161 
0162     //Create the window
0163     window = m_project->openObject(m_mobile, item, viewMode, staticObjectArgs);
0164     if (window) {
0165         m_mobile->setActiveObject(window);
0166     }
0167 
0168     m_toolbar->setRecordHandler(dynamic_cast<KexiRecordNavigatorHandler*>(window->selectedView()));
0169 
0170 #if 0
0171     if (window && !alreadyOpened) {
0172 //  window->setParent(d->tabWidget);
0173 //  KexiWindow* previousWindow = currentWindow();
0174         // Call switchToViewMode() and propertySetSwitched() again here because
0175         // this is the time when then new window is the current one - previous call did nothing.
0176         switchToViewMode(*window, window->currentViewMode());
0177         currentWindow()->selectedView()->propertySetSwitched();
0178 //  activeWindowChanged(window, previousWindow);
0179     }
0180 #endif
0181 
0182 #ifdef Q_WS_MAEMO_5
0183     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
0184 #endif
0185 
0186     return window;
0187 
0188 }
0189 
0190 bool KexiMobileMainWindow::openingAllowed(KexiPart::Item* item, Kexi::ViewMode viewMode, QString* errorMessage)
0191 {
0192     //qDebug() << viewMode;
0193     //! @todo this can be more complex once we deliver ACLs...
0194     //1 Load the part
0195     //2 Return true if the part loads AND the part supports the view mode AND the viewmode is Data
0196 
0197     KexiPart::Part * part = Kexi::partManager().part(item);
0198     if (!part) {
0199         if (errorMessage) {
0200             *errorMessage = Kexi::partManager().errorMsg();
0201         }
0202     }
0203     //qDebug() << part << item->pluginId();
0204     /*if (part)
0205         qDebug() << item->pluginId() << part->supportedUserViewModes();*/
0206     return part /*&& (part->supportedUserViewModes() & viewMode)*/ && (viewMode == Kexi::DataViewMode);
0207 }
0208 
0209 //========KexiMainWindowIFace====================
0210 
0211 void KexiMobileMainWindow::acceptProjectClosingRequested(bool *cancel)
0212 {
0213 
0214 }
0215 
0216 void KexiMobileMainWindow::acceptPropertySetEditing()
0217 {
0218 
0219 }
0220 
0221 KActionCollection* KexiMobileMainWindow::actionCollection() const
0222 {
0223     return 0;
0224 }
0225 
0226 void KexiMobileMainWindow::addToolBarAction(const QString& toolBarName, QAction* action)
0227 {
0228 
0229 }
0230 
0231 QList< QAction* > KexiMobileMainWindow::allActions() const
0232 {
0233     return QList<QAction*>();
0234 }
0235 
0236 void KexiMobileMainWindow::appendWidgetToToolbar(const QString& name, QWidget* widget)
0237 {
0238 
0239 }
0240 
0241 void KexiMobileMainWindow::beforeProjectClosing()
0242 {
0243 
0244 }
0245 
0246 tristate KexiMobileMainWindow::closeObject(KexiPart::Item* item)
0247 {
0248     return true;
0249 }
0250 
0251 tristate KexiMobileMainWindow::closeWindow(KexiWindow* window)
0252 {
0253     return true;
0254 }
0255 
0256 KexiWindow* KexiMobileMainWindow::currentWindow() const
0257 {
0258     return 0;
0259 }
0260 
0261 tristate KexiMobileMainWindow::executeCustomActionForObject(KexiPart::Item* item, const QString& actionName)
0262 {
0263     return true;
0264 }
0265 
0266 QWidget* KexiMobileMainWindow::focusWidget() const
0267 {
0268     return 0;
0269 }
0270 
0271 tristate KexiMobileMainWindow::getNewObjectInfo(KexiPart::Item* partItem, KexiPart::Part* part,
0272                                                 bool *allowOverwriting,
0273                                                 const QString& messageWhenAskingForName)
0274 {
0275     return false;
0276 }
0277 
0278 void KexiMobileMainWindow::highlightObject(const QString& mime, const QString& name)
0279 {
0280 
0281 }
0282 
0283 bool KexiMobileMainWindow::newObject(KexiPart::Info* info, bool *openingCancelled)
0284 {
0285     return false;
0286 }
0287 
0288 KexiWindow* KexiMobileMainWindow::openObject(const QString& mime, const QString& name,
0289                                              Kexi::ViewMode viewMode, bool *openingCancelled,
0290                                              QMap< QString, QVariant >* staticObjectArgs)
0291 {
0292     return 0;
0293 }
0294 
0295 tristate KexiMobileMainWindow::printItem(KexiPart::Item* item)
0296 {
0297     return false;
0298 }
0299 
0300 tristate KexiMobileMainWindow::printPreviewForItem(KexiPart::Item* item)
0301 {
0302     return false;
0303 }
0304 
0305 KexiProject* KexiMobileMainWindow::project()
0306 {
0307     return m_project;
0308 }
0309 
0310 void KexiMobileMainWindow::projectClosed()
0311 {
0312 
0313 }
0314 
0315 void KexiMobileMainWindow::propertySetSwitched(KexiWindow* window, bool force, bool preservePrevSelection, bool sortedProperties, const QByteArray& propertyToSelect)
0316 {
0317 
0318 }
0319 
0320 void KexiMobileMainWindow::registerChild(KexiWindow* window)
0321 {
0322 
0323 }
0324 
0325 tristate KexiMobileMainWindow::saveObject(KexiWindow* window, const QString& messageWhenAskingForName, bool dontAsk)
0326 {
0327     return false;
0328 }
0329 
0330 void KexiMobileMainWindow::setWidgetVisibleInToolbar(QWidget* widget, bool visible)
0331 {
0332 
0333 }
0334 
0335 tristate KexiMobileMainWindow::showPageSetupForItem(KexiPart::Item* item)
0336 {
0337     return false;
0338 }
0339 
0340 void KexiMobileMainWindow::slotObjectRenamed(const KexiPart::Item& item, const QString& oldName)
0341 {
0342 
0343 }
0344 
0345 tristate KexiMobileMainWindow::switchToViewMode(KexiWindow& window, Kexi::ViewMode viewMode)
0346 {
0347     return false;
0348 }
0349 
0350 KToolBar* KexiMobileMainWindow::toolBar(const QString& name) const
0351 {
0352     return 0;
0353 }
0354 
0355 void KexiMobileMainWindow::updatePropertyEditorInfoLabel()
0356 {
0357 
0358 }
0359 
0360 bool KexiMobileMainWindow::userMode() const
0361 {
0362     return true;
0363 }
0364 
0365 void KexiMobileMainWindow::addSearchableModel(KexiSearchableModel *model)
0366 {
0367     Q_UNUSED(model)
0368 }
0369 
0370 void KexiMobileMainWindow::removeSearchableModel(KexiSearchableModel *model)
0371 {
0372     Q_UNUSED(model)
0373 }
0374 
0375 tristate KexiMobileMainWindow::getNewObjectInfo(KexiPart::Item*, const QString&, KexiPart::Part*, bool, bool*, const QString&)
0376 {
0377     return false;
0378 }
0379 
0380 KexiWindow* KexiMobileMainWindow::openedWindowFor(const KexiPart::Item*)
0381 {
0382     return 0;
0383 }
0384 
0385 tristate KexiMobileMainWindow::saveObject(KexiWindow*, const QString&, KexiMainWindowIface::SaveObjectOptions)
0386 {
0387     return false;
0388 }
0389 
0390 KexiUserFeedbackAgent* KexiMobileMainWindow::userFeedbackAgent() const
0391 {
0392     return 0;
0393 }
0394 
0395 
0396 
0397 
0398 
0399