DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (5b81998bb7ab)

VCS Links

Line Code
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: sw=2 ts=4 et :
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */


#include <QWidget>
#include "nsMFilePicker.h"


// Use Qt Tracker API to get attachment data from tracker:
#include <QtSparql/QSparqlConnection>
#include <QtSparql/QSparqlQuery>
#include <QtSparql/QSparqlResult>

#include <MApplication>
#include <MApplicationPage>
#include <MApplicationWindow>
#include <MSceneManager>

#include <QFileInfo>
#include <QPointer>

#include <SelectSingleContentItemPage.h>
#include <SelectMultipleContentItemsPage.h>

MeegoFileDialog::MeegoFileDialog(QObject* aParent)
  : QObject(aParent)
  , mMode(Mode_Unknown)
{
}

MeegoFileDialog::~MeegoFileDialog()
{
}

int
MeegoFileDialog::exec()
{
    MApplicationPage* page = NULL;
    switch(mMode) {
    case Mode_OpenFile:
        page = createOpenFilePage();
        break;
    case Mode_OpenFiles:
        page = createOpenFilesPage();
        break;
    case Mode_OpenDirectory:
        page = createOpenDirectoryPage();
        break;
    default:
        return 0;
    }

    page->setTitle(mCaption);

    QPointer<MApplicationWindow> appWindow = new MApplicationWindow(MApplication::activeWindow());
    QObject::connect(MApplication::activeWindow(), SIGNAL(switcherEntered()), this, SLOT(backButtonClicked()));
    appWindow->show();

    // a not so pretty Hack to build up a back button
    // by using a empty scene window for a pseudo history stack
    MSceneWindow* fakeWindow = new MSceneWindow();
    // tell fake window to disappear when page back was clicked.
    connect(page, SIGNAL(backButtonClicked()), fakeWindow, SLOT(disappear()));
    // let new page appear on pseudo Application Window
    appWindow->sceneManager()->appearSceneWindowNow(page);

    // get, manipulate and set history stack
    QList<MSceneWindow*> sceneWindowHistory = appWindow->sceneManager()->pageHistory();
    sceneWindowHistory.insert(0, fakeWindow);
    appWindow->sceneManager()->setPageHistory(sceneWindowHistory);

    // start own eventloop
    QEventLoop eventLoop;
    mEventLoop = &eventLoop;
    QPointer<MeegoFileDialog> guard = this;
    (void) eventLoop.exec(QEventLoop::DialogExec);
    if (guard.isNull()) {
        return 0;
    }
    if (page) {
        page->disappear();
        delete page;
    }
    if (appWindow) {
        delete appWindow;
    }
    mEventLoop = 0;
    return 0;
}

MApplicationPage*
MeegoFileDialog::createOpenFilePage()
{
    QStringList itemType("http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#FileDataObject");

    SelectSingleContentItemPage* page =
        new SelectSingleContentItemPage(QString(),
                                        itemType);

    connect(page, SIGNAL(contentItemSelected(const QString&)),
            this, SLOT  (contentItemSelected(const QString&)));

    connect(page, SIGNAL(backButtonClicked()),
            this, SLOT  (backButtonClicked()));

    return page;
}

MApplicationPage*
MeegoFileDialog::createOpenFilesPage()
{
    QStringList itemType("http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Folder");

    SelectMultipleContentItemsPage* page = new SelectMultipleContentItemsPage(QString(), itemType);
    connect(page, SIGNAL(contentItemsSelected(const QStringList&)),
            this, SLOT  (contentItemsSelected(const QStringList&)));

    connect(page, SIGNAL(backButtonClicked()),
            this, SLOT  (backButtonClicked()));

    return page;
}

MApplicationPage*
MeegoFileDialog::createOpenDirectoryPage()
{
    QStringList itemType("http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Folder");

    SelectSingleContentItemPage* page = new SelectSingleContentItemPage(QString(), itemType);
    connect(page, SIGNAL(contentItemSelected(const QString&)),
            this, SLOT  (contentItemSelected(const QString&)));

    connect(page, SIGNAL(backButtonClicked()),
            this, SLOT  (backButtonClicked()));

    return page;
}

QStringList
MeegoFileDialog::selectedFileNames() const
{
    return mSelectedFileNames;
}

bool
MeegoFileDialog::hasSelectedFileNames() const
{
    return !mSelectedFileNames.isEmpty();
}

void
MeegoFileDialog::contentItemSelected(const QString& aContentItem)
{
    mSelectedFileNames.clear();

    if (aContentItem.isEmpty()) {
        finishEventLoop();
        return;
    }

    QUrl fileUrl = trackerIdToFilename(aContentItem);
    QFileInfo fileInfo(fileUrl.toLocalFile());
    if (fileInfo.isFile()) {
        mSelectedFileNames << fileInfo.canonicalFilePath();
    }

    finishEventLoop();
}

void
MeegoFileDialog::contentItemsSelected(const QStringList& aContentItems)
{
    mSelectedFileNames.clear();

    foreach (QString contentItem, aContentItems) {
        QUrl fileUrl = trackerIdToFilename(contentItem);
        QFileInfo fileInfo(fileUrl.toLocalFile());
        if (fileInfo.isFile()) {
            mSelectedFileNames << fileInfo.canonicalFilePath();
        }
    }

    finishEventLoop();
}

void
MeegoFileDialog::backButtonClicked()
{
    mSelectedFileNames.clear();
    finishEventLoop();
}

/* static */ QString
MeegoFileDialog::getOpenFileName(QWidget* parent,
                                 const QString& caption,
                                 const QString& dir,
                                 const QString& filter)
{
    QPointer<MeegoFileDialog> picker = new MeegoFileDialog(parent);

    QString result;
    picker->mMode = MeegoFileDialog::Mode_OpenFile;
    picker->mCaption = caption;
    picker->exec();

    // Need separate 'if' because picker might have been destroyed by its parent during exec()
    if (picker) {
        if (picker->hasSelectedFileNames()) {
            result = picker->selectedFileNames().first();
        }
        delete picker;
    }

    return result;
}

/* static */ QString
MeegoFileDialog::getExistingDirectory(QWidget* parent,const QString& caption, const QString& dir)
{
    QPointer<MeegoFileDialog> picker = new MeegoFileDialog(parent);

    QString result;
    picker->mMode = MeegoFileDialog::Mode_OpenDirectory;
    picker->mCaption = caption;
    picker->exec();

    // Need separate 'if' because picker might have been destroyed by its parent during exec()
    if (picker) {
        if (picker->hasSelectedFileNames()) {
            result = picker->selectedFileNames().first();
        }
        delete picker;
    }

    return result;
}

/* static */ QStringList
MeegoFileDialog::getOpenFileNames(QWidget* parent,
                                  const QString& caption,
                                  const QString& dir,
                                  const QString& filter)
{
    QPointer<MeegoFileDialog> picker = new MeegoFileDialog(parent);

    QStringList result;
    picker->mMode = MeegoFileDialog::Mode_OpenFiles;
    picker->mCaption = caption;
    picker->exec();

    // Need separate 'if' because picker might have been destroyed by its parent during exec()
    if (picker) {
        if (picker->hasSelectedFileNames()) {
            result = picker->selectedFileNames();
        }
        delete picker;
    }

    return result;
}

QUrl
MeegoFileDialog::trackerIdToFilename(const QString& trackerId)
{
    QSparqlQuery query("SELECT ?u WHERE { ?:tUri nie:url ?u . }");
    query.bindValue("tUri", QUrl(trackerId)); // That puts <trackerId> into the query
    QSparqlConnection connection("QTRACKER"); // Exact string needed to make connection to tracker
    QSparqlResult* result = connection.exec(query);

    result->waitForFinished();
    result->first();

    QUrl resultFile;
    if (result->isValid()) {
        resultFile = QUrl(result->binding(0).value().toString());
    }
    delete result;

    return resultFile;
}