File indexing completed on 2024-05-12 16:40:16

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     qDebug() << "KexiMobileMainWindow::openObject";
0142 
0143     KexiWindow *window = 0;
0144 
0145     if (!openingAllowed(item, viewMode, errorMessage)) {
0146         if (errorMessage)
0147             *errorMessage = xi18nc(
0148                                 "@info opening is not allowed in \"data view/design view/text view\" mode",
0149                                 "opening is not allowed in <resource>%1</resource> mode",
0150                                 Kexi::nameForViewMode(viewMode));
0151         *openingCancelled = true;
0152         return 0;
0153     }
0154     qDebug() << m_project << item;
0155 
0156     if (!m_project || !item)
0157         return 0;
0158 
0159 //!TODO Move this to KexiUtils::WaitCursor
0160 #ifdef Q_WS_MAEMO_5
0161     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
0162 #endif
0163 
0164     //Create the window
0165     window = m_project->openObject(m_mobile, item, viewMode, staticObjectArgs);
0166     if (window) {
0167         m_mobile->setActiveObject(window);
0168     }
0169 
0170     m_toolbar->setRecordHandler(dynamic_cast<KexiRecordNavigatorHandler*>(window->selectedView()));
0171 
0172 #if 0
0173     if (window && !alreadyOpened) {
0174 //  window->setParent(d->tabWidget);
0175 //  KexiWindow* previousWindow = currentWindow();
0176         // Call switchToViewMode() and propertySetSwitched() again here because
0177         // this is the time when then new window is the current one - previous call did nothing.
0178         switchToViewMode(*window, window->currentViewMode());
0179         currentWindow()->selectedView()->propertySetSwitched();
0180 //  activeWindowChanged(window, previousWindow);
0181     }
0182 #endif
0183 
0184 #ifdef Q_WS_MAEMO_5
0185     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
0186 #endif
0187 
0188     return window;
0189 
0190 }
0191 
0192 bool KexiMobileMainWindow::openingAllowed(KexiPart::Item* item, Kexi::ViewMode viewMode, QString* errorMessage)
0193 {
0194     qDebug() << viewMode;
0195     //! @todo this can be more complex once we deliver ACLs...
0196     //1 Load the part
0197     //2 Return true if the part loads AND the part supports the view mode AND the viewmode is Data
0198 
0199     KexiPart::Part * part = Kexi::partManager().part(item);
0200     if (!part) {
0201         if (errorMessage) {
0202             *errorMessage = Kexi::partManager().errorMsg();
0203         }
0204     }
0205     qDebug() << part << item->pluginId();
0206     /*if (part)
0207         qDebug() << item->pluginId() << part->supportedUserViewModes();*/
0208     return part /*&& (part->supportedUserViewModes() & viewMode)*/ && (viewMode == Kexi::DataViewMode);
0209 }
0210 
0211 //========KexiMainWindowIFace====================
0212 
0213 void KexiMobileMainWindow::acceptProjectClosingRequested(bool *cancel)
0214 {
0215 
0216 }
0217 
0218 void KexiMobileMainWindow::acceptPropertySetEditing()
0219 {
0220 
0221 }
0222 
0223 KActionCollection* KexiMobileMainWindow::actionCollection() const
0224 {
0225     return 0;
0226 }
0227 
0228 void KexiMobileMainWindow::addToolBarAction(const QString& toolBarName, QAction* action)
0229 {
0230 
0231 }
0232 
0233 QList< QAction* > KexiMobileMainWindow::allActions() const
0234 {
0235     return QList<QAction*>();
0236 }
0237 
0238 void KexiMobileMainWindow::appendWidgetToToolbar(const QString& name, QWidget* widget)
0239 {
0240 
0241 }
0242 
0243 void KexiMobileMainWindow::beforeProjectClosing()
0244 {
0245 
0246 }
0247 
0248 tristate KexiMobileMainWindow::closeObject(KexiPart::Item* item)
0249 {
0250     return true;
0251 }
0252 
0253 tristate KexiMobileMainWindow::closeWindow(KexiWindow* window)
0254 {
0255     return true;
0256 }
0257 
0258 KexiWindow* KexiMobileMainWindow::currentWindow() const
0259 {
0260     return 0;
0261 }
0262 
0263 tristate KexiMobileMainWindow::executeCustomActionForObject(KexiPart::Item* item, const QString& actionName)
0264 {
0265     return true;
0266 }
0267 
0268 QWidget* KexiMobileMainWindow::focusWidget() const
0269 {
0270     return 0;
0271 }
0272 
0273 tristate KexiMobileMainWindow::getNewObjectInfo(KexiPart::Item* partItem, KexiPart::Part* part,
0274                                                 bool *allowOverwriting,
0275                                                 const QString& messageWhenAskingForName)
0276 {
0277     return false;
0278 }
0279 
0280 void KexiMobileMainWindow::highlightObject(const QString& mime, const QString& name)
0281 {
0282 
0283 }
0284 
0285 bool KexiMobileMainWindow::newObject(KexiPart::Info* info, bool *openingCancelled)
0286 {
0287     return false;
0288 }
0289 
0290 KexiWindow* KexiMobileMainWindow::openObject(const QString& mime, const QString& name,
0291                                              Kexi::ViewMode viewMode, bool *openingCancelled,
0292                                              QMap< QString, QVariant >* staticObjectArgs)
0293 {
0294     return 0;
0295 }
0296 
0297 tristate KexiMobileMainWindow::printItem(KexiPart::Item* item)
0298 {
0299     return false;
0300 }
0301 
0302 tristate KexiMobileMainWindow::printPreviewForItem(KexiPart::Item* item)
0303 {
0304     return false;
0305 }
0306 
0307 KexiProject* KexiMobileMainWindow::project()
0308 {
0309     return m_project;
0310 }
0311 
0312 void KexiMobileMainWindow::projectClosed()
0313 {
0314 
0315 }
0316 
0317 void KexiMobileMainWindow::propertySetSwitched(KexiWindow* window, bool force, bool preservePrevSelection, bool sortedProperties, const QByteArray& propertyToSelect)
0318 {
0319 
0320 }
0321 
0322 void KexiMobileMainWindow::registerChild(KexiWindow* window)
0323 {
0324 
0325 }
0326 
0327 tristate KexiMobileMainWindow::saveObject(KexiWindow* window, const QString& messageWhenAskingForName, bool dontAsk)
0328 {
0329     return false;
0330 }
0331 
0332 void KexiMobileMainWindow::setWidgetVisibleInToolbar(QWidget* widget, bool visible)
0333 {
0334 
0335 }
0336 
0337 tristate KexiMobileMainWindow::showPageSetupForItem(KexiPart::Item* item)
0338 {
0339     return false;
0340 }
0341 
0342 void KexiMobileMainWindow::slotObjectRenamed(const KexiPart::Item& item, const QString& oldName)
0343 {
0344 
0345 }
0346 
0347 tristate KexiMobileMainWindow::switchToViewMode(KexiWindow& window, Kexi::ViewMode viewMode)
0348 {
0349     return false;
0350 }
0351 
0352 KToolBar* KexiMobileMainWindow::toolBar(const QString& name) const
0353 {
0354     return 0;
0355 }
0356 
0357 void KexiMobileMainWindow::updatePropertyEditorInfoLabel(const QString& textToDisplayForNullSet)
0358 {
0359 
0360 }
0361 
0362 bool KexiMobileMainWindow::userMode() const
0363 {
0364     return true;
0365 }
0366 
0367 void KexiMobileMainWindow::addSearchableModel(KexiSearchableModel *model)
0368 {
0369     Q_UNUSED(model)
0370 }
0371 
0372 void KexiMobileMainWindow::removeSearchableModel(KexiSearchableModel *model)
0373 {
0374     Q_UNUSED(model)
0375 }
0376 
0377 tristate KexiMobileMainWindow::getNewObjectInfo(KexiPart::Item*, const QString&, KexiPart::Part*, bool, bool*, const QString&)
0378 {
0379     return false;
0380 }
0381 
0382 KexiWindow* KexiMobileMainWindow::openedWindowFor(const KexiPart::Item*)
0383 {
0384     return 0;
0385 }
0386 
0387 tristate KexiMobileMainWindow::saveObject(KexiWindow*, const QString&, KexiMainWindowIface::SaveObjectOptions)
0388 {
0389     return false;
0390 }
0391 
0392 KexiUserFeedbackAgent* KexiMobileMainWindow::userFeedbackAgent() const
0393 {
0394     return 0;
0395 }
0396 
0397 
0398 
0399 
0400 
0401