diff --git a/addons/filetree/autotests/filetree_model_test.cpp b/addons/filetree/autotests/filetree_model_test.cpp index 2f594b560..f95b5bdb2 100644 --- a/addons/filetree/autotests/filetree_model_test.cpp +++ b/addons/filetree/autotests/filetree_model_test.cpp @@ -1,915 +1,915 @@ /* This file is part of the KDE project * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "filetree_model_test.h" #include "katefiletreemodel.h" #include "document_dummy.h" #include QTEST_GUILESS_MAIN(FileTreeModelTest) //BEGIN ResultNode class ResultNode { public: ResultNode() : name(), dir(true), children() {} // root node ResultNode(const ResultNode &other) : name(other.name), dir(other.dir), children(other.children) {} ResultNode(const char *_name, const bool _dir = false) : ResultNode(QString::fromLatin1(_name), _dir) {} ResultNode(const QString &_name, const bool _dir = false) : name(_name), dir(_dir), children() {} - ResultNode &operator<<(ResultNode node) { children << node; return *this; } + ResultNode &operator<<(const ResultNode &node) { children << node; return *this; } bool operator!=(const ResultNode &other) const { return !(*this == other); } bool operator==(const ResultNode &other) const { return (other.name == name) && (other.dir == dir) && (other.children == children); } friend QDebug operator<< (QDebug s, const ResultNode &node) { s << node.toString(); return s; } friend void debugOutput(QString &s, const ResultNode &rootNode, const int level = 0) { for (int i = 0; i < level; i++) { s += QLatin1String(" "); } const QString name = rootNode.name.isEmpty() ? QStringLiteral("ROOT") : rootNode.name; s += QLatin1String("( ") + name; if (rootNode.dir) { s += QLatin1String(", {D}"); } if (rootNode.children.isEmpty()) { s += QLatin1String(" )"); } else { s += QLatin1String(",\n"); for (int i = 0; i < rootNode.children.size(); i++) { const ResultNode &node = rootNode.children[i]; debugOutput(s, node, level + 1); if ((i + 1) < rootNode.children.size()) { s+= QLatin1String("\n"); } } s += (level == 0) ? QLatin1String("\n);") : QLatin1String(")"); } } QString toString() const { QString out; debugOutput(out, *this, 0); return out; } QString name; bool dir; QList children; }; Q_DECLARE_METATYPE(ResultNode) namespace QTest { inline bool qCompare(const ResultNode &t1, const ResultNode &t2, const char *actual, const char *expected, const char *file, int line) { /* compare_helper is not helping that much, we need to prepare copy of data */ const QByteArray a = t1.toString().toLatin1(); const QByteArray b = t2.toString().toLatin1(); char *val1 = new char[a.size() + 1]; char *val2 = new char[b.size() + 1]; memcpy(val1, a.constData(), a.size() + 1); memcpy(val2, b.constData(), b.size() + 1); return compare_helper(t1 == t2, "Compared ResultNode trees are not the same", val1, val2, actual, expected, file, line); } } //END ResultNode void FileTreeModelTest::initTestCase() {} void FileTreeModelTest::cleanupTestCase() {} void FileTreeModelTest::init() {} void FileTreeModelTest::cleanup() {} void FileTreeModelTest::basic() { QScopedPointer d1(new DummyDocument()); QScopedPointer d2(new DummyDocument()); KateFileTreeModel m(this); QCOMPARE(m.rowCount(QModelIndex()), 0); m.documentOpened(d1.data()); QCOMPARE(m.rowCount(QModelIndex()), 1); m.documentOpened(d2.data()); QCOMPARE(m.rowCount(QModelIndex()), 2); } void FileTreeModelTest::buildTree_data() { QTest::addColumn>("documents"); QTest::addColumn("nodes"); QTest::newRow("easy") << ( QList() << new DummyDocument("file:///a/foo.txt") ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo.txt")) ); QTest::newRow("two") << ( QList() << new DummyDocument("file:///a/foo.txt") << new DummyDocument("file:///a/bar.txt") ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo.txt") << ResultNode("bar.txt")) ); QTest::newRow("strangers") << ( QList() << new DummyDocument("file:///a/foo.txt") << new DummyDocument("file:///b/bar.txt") ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo.txt")) << (ResultNode("b", true) << ResultNode("bar.txt")) ); QTest::newRow("lvl1 strangers") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo.txt")) << (ResultNode("b", true) << ResultNode("bar.txt")) ); QTest::newRow("multiples") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") << new DummyDocument("file:///c/a/bar.txt") ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo.txt") << ResultNode("bar.txt")) << (ResultNode("b", true) << ResultNode("bar.txt")) ); QTest::newRow("stairs") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/bar.txt") ) << ( ResultNode() << (ResultNode("c", true) << (ResultNode("a", true) << ResultNode("foo.txt")) << ResultNode("bar.txt")) ); QTest::newRow("reverse stairs") << ( QList() << new DummyDocument("file:///c/bar.txt") << new DummyDocument("file:///c/a/foo.txt") ) << ( ResultNode() << (ResultNode("c", true) << ResultNode("bar.txt") << (ResultNode("a", true) << ResultNode("foo.txt"))) ); QTest::newRow("matching") << ( QList() << new DummyDocument("file:///a/x/foo.txt") << new DummyDocument("file:///b/x/bar.txt") ) << ( ResultNode() << (ResultNode("a", true) << (ResultNode("x", true) << ResultNode("foo.txt"))) << (ResultNode("b", true) << (ResultNode("x", true) << ResultNode("bar.txt"))) ); QTest::newRow("matching even more") << ( QList() << new DummyDocument("file:///a/x/y/z/foo.txt") << new DummyDocument("file:///b/x/y/z/bar.txt") ) << ( ResultNode() << (ResultNode("a", true) << (ResultNode("x", true) << (ResultNode("y", true) << (ResultNode("z", true) << ResultNode("foo.txt"))))) << (ResultNode("b", true) << (ResultNode("x", true) << (ResultNode("y", true) << (ResultNode("z", true) << ResultNode("bar.txt"))))) ); QTest::newRow("matching with booby trap") << ( QList() << new DummyDocument("file:///x/y/foo.txt") << new DummyDocument("file:///c/x/bar.txt") << new DummyDocument("file:///d/y/baz.txt") ) << ( ResultNode() << (ResultNode("x", true) << (ResultNode("y", true) << ResultNode("foo.txt"))) << (ResultNode("d", true) << (ResultNode("y", true) << ResultNode("baz.txt"))) << (ResultNode("c", true) << (ResultNode("x", true) << ResultNode("bar.txt"))) ); QTest::newRow("branches") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") << new DummyDocument("file:///d/a/foo.txt") ) << ( ResultNode() << (ResultNode("c", true) << (ResultNode("a", true) << ResultNode("foo.txt")) << (ResultNode("b", true) << ResultNode("bar.txt"))) << (ResultNode("d", true) << (ResultNode("a", true) << ResultNode("foo.txt"))) ); QTest::newRow("branches (more)") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") << new DummyDocument("file:///c/c/bar.txt") << new DummyDocument("file:///d/a/foo.txt") ) << ( ResultNode() << (ResultNode("c", true) << (ResultNode("a", true) << ResultNode("foo.txt")) << (ResultNode("b", true) << ResultNode("bar.txt")) << (ResultNode("c", true) << ResultNode("bar.txt"))) << (ResultNode("d", true) << (ResultNode("a", true) << ResultNode("foo.txt"))) ); QTest::newRow("bug347578") << ( QList() << new DummyDocument("file:///f/g/a/b/c/d/e.txt") << new DummyDocument("file:///f/g/a/t/b/c/d/e.txt") ) << ( ResultNode() << (ResultNode("a", true) << (ResultNode("b", true) << (ResultNode("c", true) << (ResultNode("d", true) << ResultNode("e.txt")))) << (ResultNode("t", true) << (ResultNode("b", true) << (ResultNode("c", true) << (ResultNode("d", true) << ResultNode("e.txt")))))) ); QTest::newRow("levels") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") << new DummyDocument("file:///d/foo.txt") ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo.txt")) << (ResultNode("b", true) << ResultNode("bar.txt")) << (ResultNode("d", true) << ResultNode("foo.txt")) ); QTest::newRow("remote simple") << ( QList() << new DummyDocument("http://example.org/foo.txt") ) << ( ResultNode() << (ResultNode("[example.org]", true) << ResultNode("foo.txt")) ); QTest::newRow("remote nested") << ( QList() << new DummyDocument("http://example.org/a/foo.txt") ) << ( ResultNode() << (ResultNode("[example.org]a", true) << ResultNode("foo.txt")) ); /* NOTE: this one is also not completely ok, is it? * on other hand, it would get confusing or overly leveled if opening * something like http://example.org/a/b/c/d/e/f/g.txt */ QTest::newRow("remote diverge") << ( QList() << new DummyDocument("http://example.org/a/foo.txt") << new DummyDocument("http://example.org/b/foo.txt") ) << ( ResultNode() << (ResultNode("[example.org]a", true) << ResultNode("foo.txt")) << (ResultNode("[example.org]b", true) << ResultNode("foo.txt")) ); } void FileTreeModelTest::buildTree() { KateFileTreeModel m(this); QFETCH(QList, documents); QFETCH(ResultNode, nodes); foreach(DummyDocument *doc, documents) { m.documentOpened(doc); } ResultNode root; walkTree(m, QModelIndex(), root); QCOMPARE(root, nodes); qDeleteAll(documents); } void FileTreeModelTest::buildTreeBatch_data() { // the easiest way to verify the equality of those two calls:) buildTree_data(); } void FileTreeModelTest::buildTreeBatch() { KateFileTreeModel m(this); QFETCH(QList, documents); QFETCH(ResultNode, nodes); QList list; foreach(DummyDocument *doc, documents) { list << doc; } m.documentsOpened(list); ResultNode root; walkTree(m, QModelIndex(), root); QCOMPARE(root, nodes); qDeleteAll(documents); } void FileTreeModelTest::buildTreeBatchPrefill_data() { QTest::addColumn>("prefill"); QTest::addColumn>("documents"); QTest::addColumn("nodes"); QTest::newRow("easy") << ( QList() << new DummyDocument("file:///a/foo.txt") ) << ( QList() << new DummyDocument("file:///a/bar.txt") ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo.txt") << ResultNode("bar.txt")) ); QTest::newRow("split") << ( QList() << new DummyDocument("file:///a/foo.txt") ) << ( QList() << new DummyDocument("file:///b/foo.txt") ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo.txt")) << (ResultNode("b", true) << ResultNode("foo.txt")) ); } void FileTreeModelTest::buildTreeBatchPrefill() { KateFileTreeModel m(this); QFETCH(QList, prefill); QFETCH(QList, documents); QFETCH(ResultNode, nodes); foreach(DummyDocument *doc, prefill) { m.documentOpened(doc); } QList list; foreach(DummyDocument *doc, documents) { list << doc; } m.documentsOpened(list); ResultNode root; walkTree(m, QModelIndex(), root); QCOMPARE(root, nodes); qDeleteAll(prefill); qDeleteAll(documents); } void FileTreeModelTest::walkTree(KateFileTreeModel &model, const QModelIndex &rootIndex, ResultNode &rootNode) { if (!model.hasChildren(rootIndex)) { return; } const int rows = model.rowCount(rootIndex); for (int i = 0; i < rows; i++) { const QModelIndex idx = model.index(i, 0, rootIndex); ResultNode node(model.data(idx).toString(), model.isDir(idx)); walkTree(model, idx, node); rootNode << node; } } void FileTreeModelTest::buildTreeFullPath_data() { QTest::addColumn>("documents"); QTest::addColumn("nodes"); QTest::newRow("two") << ( QList() << new DummyDocument("file:///a/foo.txt") << new DummyDocument("file:///a/bar.txt") ) << ( ResultNode() << (ResultNode("/a", true) << ResultNode("foo.txt") << ResultNode("bar.txt")) ); QTest::newRow("multiples") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") << new DummyDocument("file:///c/a/bar.txt") ) << ( ResultNode() << (ResultNode("/c/a", true) << ResultNode("foo.txt") << ResultNode("bar.txt")) << (ResultNode("/c/b", true) << ResultNode("bar.txt")) ); /* This one and the case after can get a little bit tricky and * in some situation could end up in little bit confusing layout. * current root merge algorithm sees the divergent paths, so it * doesn't invoke merging. QTest::newRow("branches") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") ) << ( ResultNode() << (ResultNode("/c", true) << (ResultNode("a", true) << ResultNode("foo.txt")) << (ResultNode("b", true) << ResultNode("bar.txt"))) ); */ /* QTest::newRow("levels") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") << new DummyDocument("file:///d/foo.txt") ) << ( ResultNode() << (ResultNode("/c", true) << (ResultNode("a", true) << ResultNode("foo.txt")) << (ResultNode("b", true) << ResultNode("bar.txt"))) << (ResultNode("/d", true) << ResultNode("foo.txt")) ); */ QTest::newRow("remote simple") << ( QList() << new DummyDocument("http://example.org/foo.txt") ) << ( ResultNode() << (ResultNode("[example.org]", true) << ResultNode("foo.txt")) ); QTest::newRow("remote nested") << ( QList() << new DummyDocument("http://example.org/a/b/foo.txt") ) << ( ResultNode() << (ResultNode("[example.org]/a/b", true) << ResultNode("foo.txt")) ); /* NOTE: see the similar testcase in buildTree */ QTest::newRow("remote diverge") << ( QList() << new DummyDocument("http://example.org/c/a/foo.txt") << new DummyDocument("http://example.org/c/b/foo.txt") ) << ( ResultNode() << (ResultNode("[example.org]/c/a", true) << ResultNode("foo.txt")) << (ResultNode("[example.org]/c/b", true) << ResultNode("foo.txt")) ); } void FileTreeModelTest::buildTreeFullPath() { KateFileTreeModel m(this); m.setShowFullPathOnRoots(true); QFETCH(QList, documents); QFETCH(ResultNode, nodes); foreach(DummyDocument *doc, documents) { m.documentOpened(doc); } ResultNode root; walkTree(m, QModelIndex(), root); QCOMPARE(root, nodes); qDeleteAll(documents); } void FileTreeModelTest::listMode_data() { QTest::addColumn>("documents"); QTest::addColumn("nodes"); QTest::newRow("easy") << ( QList() << new DummyDocument("file:///a/foo.txt") ) << ( ResultNode() << ResultNode("foo.txt") ); QTest::newRow("two") << ( QList() << new DummyDocument("file:///a/foo.txt") << new DummyDocument("file:///a/bar.txt") ) << ( ResultNode() << ResultNode("foo.txt") << ResultNode("bar.txt") ); QTest::newRow("multiples") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") << new DummyDocument("file:///c/a/bar.txt") ) << ( ResultNode() << ResultNode("foo.txt") << ResultNode("bar.txt") << ResultNode("bar.txt") ); QTest::newRow("remote diverge") << ( QList() << new DummyDocument("http://example.org/a/foo.txt") << new DummyDocument("http://example.org/b/foo.txt") ) << ( ResultNode() << ResultNode("[example.org]foo.txt") << ResultNode("[example.org]foo.txt") ); } void FileTreeModelTest::listMode() { KateFileTreeModel m(this); m.setListMode(true); QFETCH(QList, documents); QFETCH(ResultNode, nodes); foreach(DummyDocument *doc, documents) { m.documentOpened(doc); } ResultNode root; walkTree(m, QModelIndex(), root); QCOMPARE(root, nodes); qDeleteAll(documents); } void FileTreeModelTest::deleteDocument_data() { QTest::addColumn>("documents"); QTest::addColumn>("remove"); QTest::addColumn("nodes"); QTest::newRow("empty") << ( QList() << new DummyDocument("file:///a/foo.txt") ) << ( QList() << 0 ) << ( ResultNode() ); QTest::newRow("two") << ( QList() << new DummyDocument("file:///a/foo.txt") << new DummyDocument("file:///a/bar.txt") ) << ( QList() << 0 ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("bar.txt")) ); QTest::newRow("multiple") << ( QList() << new DummyDocument("file:///a/foo0.txt") << new DummyDocument("file:///a/foo1.txt") << new DummyDocument("file:///a/foo2.txt") << new DummyDocument("file:///a/foo3.txt") << new DummyDocument("file:///a/foo4.txt") << new DummyDocument("file:///a/foo5.txt") << new DummyDocument("file:///a/foo6.txt") << new DummyDocument("file:///a/foo7.txt") ) << ( QList() << 1 << 2 << 4 << 6 ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo0.txt") << ResultNode("foo3.txt") << ResultNode("foo5.txt") << ResultNode("foo7.txt")) ); QTest::newRow("strangers") << ( QList() << new DummyDocument("file:///a/foo.txt") << new DummyDocument("file:///b/bar.txt") ) << ( QList() << 1 ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo.txt")) ); QTest::newRow("branches") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") << new DummyDocument("file:///d/a/foo.txt") ) << ( QList() << 1 ) << ( ResultNode() << (ResultNode("c", true) << (ResultNode("a", true) << ResultNode("foo.txt"))) << (ResultNode("d", true) << (ResultNode("a", true) << ResultNode("foo.txt"))) ); QTest::newRow("levels") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") << new DummyDocument("file:///d/foo.txt") ) << ( QList() << 0 ) << ( ResultNode() << (ResultNode("b", true) << ResultNode("bar.txt")) << (ResultNode("d", true) << ResultNode("foo.txt")) ); QTest::newRow("levels extra") << ( QList() << new DummyDocument("file:///c/a/foo.txt") << new DummyDocument("file:///c/b/bar.txt") << new DummyDocument("file:///d/foo.txt") ) << ( QList() << 2 ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo.txt")) << (ResultNode("b", true) << ResultNode("bar.txt")) ); QTest::newRow("remote diverge") << ( QList() << new DummyDocument("http://example.org/a/foo.txt") << new DummyDocument("http://example.org/b/foo.txt") ) << ( QList() << 1 ) << ( ResultNode() << (ResultNode("[example.org]a", true) << ResultNode("foo.txt")) ); } void FileTreeModelTest::deleteDocument() { KateFileTreeModel m(this); QFETCH(QList, documents); QFETCH(QList, remove); QFETCH(ResultNode, nodes); foreach(DummyDocument *doc, documents) { m.documentOpened(doc); } foreach(const int &index, remove) { m.documentClosed(documents[index]); } ResultNode root; walkTree(m, QModelIndex(), root); QCOMPARE(root, nodes); qDeleteAll(documents); } void FileTreeModelTest::deleteDocumentBatch_data() { QTest::addColumn>("documents"); QTest::addColumn>("remove"); QTest::addColumn>("fail"); QTest::addColumn("nodes"); QTest::newRow("neo") << ( QList() << new DummyDocument("file:///a/foo0.txt") << new DummyDocument("file:///a/foo1.txt") << new DummyDocument("file:///a/foo2.txt") << new DummyDocument("file:///a/foo3.txt") << new DummyDocument("file:///a/foo4.txt") << new DummyDocument("file:///a/foo5.txt") << new DummyDocument("file:///a/foo6.txt") << new DummyDocument("file:///a/foo7.txt") ) << ( QList() << 1 << 2 << 4 << 6 ) << ( QList() << 2 << 4 ) << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo0.txt") << ResultNode("foo2.txt") << ResultNode("foo3.txt") << ResultNode("foo4.txt") << ResultNode("foo5.txt") << ResultNode("foo7.txt")) ); } void FileTreeModelTest::deleteDocumentBatch() { KateFileTreeModel m(this); QFETCH(QList, documents); QFETCH(QList, remove); QFETCH(QList, fail); QFETCH(ResultNode, nodes); foreach (DummyDocument *doc, documents) { m.documentOpened(doc); } QList removing; foreach (const int &index, remove) { removing << documents[index]; } m.slotAboutToDeleteDocuments(removing); foreach (const int &index, remove) { if (!fail.contains(index)) { m.documentClosed(documents[index]); } } removing.clear(); foreach (const int &index, fail) { removing << documents[index]; } m.slotDocumentsDeleted(removing); ResultNode root; walkTree(m, QModelIndex(), root); QCOMPARE(root, nodes); qDeleteAll(documents); } void FileTreeModelTest::rename_data() { QTest::addColumn>("documents"); QTest::addColumn("rename_idx"); QTest::addColumn("rename_url"); QTest::addColumn("nodes"); QTest::newRow("empty") << ( QList() << new DummyDocument() ) << 0 << QStringLiteral("file:///a/foo.txt") << ( ResultNode() << (ResultNode("a", true) << ResultNode("foo.txt")) ); QTest::newRow("moving") << ( QList() << new DummyDocument("file:///a/foo.txt") ) << 0 << QStringLiteral("file:///b/foo.txt") << ( ResultNode() << (ResultNode("b", true) << ResultNode("foo.txt")) ); QTest::newRow("splitting") << ( QList() << new DummyDocument("file:///a/foo.txt") << new DummyDocument("file:///a/bar.txt") ) << 0 << QStringLiteral("file:///b/foo.txt") << ( ResultNode() << (ResultNode("a", true) << ResultNode("bar.txt")) << (ResultNode("b", true) << ResultNode("foo.txt")) ); } void FileTreeModelTest::rename() { KateFileTreeModel m(this); QFETCH(QList, documents); QFETCH(int, rename_idx); QFETCH(QString, rename_url); QFETCH(ResultNode, nodes); foreach (DummyDocument *doc, documents) { m.documentOpened(doc); } documents[rename_idx]->setUrl(rename_url); m.documentNameChanged(documents[rename_idx]); ResultNode root; walkTree(m, QModelIndex(), root); QCOMPARE(root, nodes); qDeleteAll(documents); } // kate: space-indent on; indent-width 2; replace-tabs on; diff --git a/addons/filetree/katefiletree.h b/addons/filetree/katefiletree.h index f349ba55c..b33452bd6 100644 --- a/addons/filetree/katefiletree.h +++ b/addons/filetree/katefiletree.h @@ -1,116 +1,116 @@ /* This file is part of the KDE project Copyright (C) 2010 Thomas Fjellstrom This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef KATE_FILETREE_H #define KATE_FILETREE_H #include #include #include namespace KTextEditor { class Document; } class QActionGroup; class KateFileTree: public QTreeView { Q_OBJECT public: KateFileTree(QWidget *parent); ~KateFileTree() override; void setModel(QAbstractItemModel *model) override; public Q_SLOTS: void slotDocumentClose(); void slotExpandRecursive(); void slotCollapseRecursive(); void slotDocumentCloseOther(); void slotDocumentReload(); void slotCopyFilename(); void slotCurrentChanged(const QModelIndex ¤t, const QModelIndex &previous); void slotDocumentFirst(); void slotDocumentLast(); void slotDocumentNext(); void slotDocumentPrev(); void slotPrintDocument(); void slotPrintDocumentPreview(); void slotResetHistory(); void slotDocumentDelete(); protected: void contextMenuEvent(QContextMenuEvent *event) override; Q_SIGNALS: void closeDocument(KTextEditor::Document *); void activateDocument(KTextEditor::Document *); - void openDocument(QUrl); + void openDocument(const QUrl&); void viewModeChanged(bool treeMode); void sortRoleChanged(int); private Q_SLOTS: void mouseClicked(const QModelIndex &index); void slotTreeMode(); void slotListMode(); void slotSortName(); void slotSortPath(); void slotSortOpeningOrder(); void slotFixOpenWithMenu(); void slotOpenWithMenuAction(QAction *a); void slotRenameFile(); private: QAction *setupOption(QActionGroup *group, const QIcon &, const QString &, const QString &, const char *slot, bool checked = false); private: QAction *m_filelistCloseDocument; QAction *m_filelistExpandRecursive; QAction *m_filelistCollapseRecursive; QAction *m_filelistCloseOtherDocument; QAction *m_filelistReloadDocument; QAction *m_filelistCopyFilename; QAction *m_filelistRenameFile; QAction *m_filelistPrintDocument; QAction *m_filelistPrintDocumentPreview; QAction *m_filelistDeleteDocument; QAction *m_treeModeAction; QAction *m_listModeAction; QAction *m_sortByFile; QAction *m_sortByPath; QAction *m_sortByOpeningOrder; QAction *m_resetHistory; QPersistentModelIndex m_previouslySelected; QPersistentModelIndex m_indexContextMenu; }; #endif // KATE_FILETREE_H diff --git a/addons/katesql/connectionmodel.cpp b/addons/katesql/connectionmodel.cpp index 31a7b4207..690c67bdc 100644 --- a/addons/katesql/connectionmodel.cpp +++ b/addons/katesql/connectionmodel.cpp @@ -1,153 +1,153 @@ /* Copyright (C) 2010 Marco Mentasti This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "connectionmodel.h" #include #include #include #include #include #include #include #include ConnectionModel::ConnectionModel(QObject *parent) : QAbstractListModel(parent) { m_icons[Connection::UNKNOWN] = QIcon::fromTheme(QStringLiteral("user-offline")); m_icons[Connection::ONLINE] = QIcon::fromTheme(QStringLiteral("user-online")); m_icons[Connection::OFFLINE] = QIcon::fromTheme(QStringLiteral("user-offline")); m_icons[Connection::REQUIRE_PASSWORD] = QIcon::fromTheme(QStringLiteral("user-invisible")); } ConnectionModel::~ConnectionModel() { } int ConnectionModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return m_connections.count(); } QVariant ConnectionModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); const QString key = m_connections.keys().at(index.row()); switch (role) { case Qt::DisplayRole: return QVariant(m_connections.value(key).name); break; case Qt::UserRole: return qVariantFromValue(m_connections.value(key)); break; case Qt::DecorationRole: return m_icons[m_connections.value(key).status]; break; case Qt::SizeHintRole: { const QFontMetrics metrics(QFontDatabase::systemFont(QFontDatabase::GeneralFont)); return QSize(metrics.width(m_connections.value(key).name), 22); } break; default: return QVariant(); break; } return QVariant(); } -int ConnectionModel::addConnection( Connection conn ) +int ConnectionModel::addConnection( const Connection &conn ) { /// FIXME if (m_connections.contains(conn.name)) { qDebug() << "a connection named" << conn.name << "already exists!"; return -1; } int pos = m_connections.count(); beginInsertRows(QModelIndex(), pos, pos); m_connections[conn.name] = conn; endInsertRows(); return m_connections.keys().indexOf(conn.name); } void ConnectionModel::removeConnection( const QString &name ) { int pos = m_connections.keys().indexOf(name); beginRemoveRows(QModelIndex(), pos, pos); m_connections.remove(name); endRemoveRows(); } int ConnectionModel::indexOf(const QString &name) { return m_connections.keys().indexOf(name); } Connection::Status ConnectionModel::status(const QString &name) const { if (!m_connections.contains(name)) return Connection::UNKNOWN; return m_connections[name].status; } void ConnectionModel::setStatus(const QString &name, const Connection::Status status) { if (!m_connections.contains(name)) return; m_connections[name].status = status; const int i = indexOf(name); emit dataChanged(index(i), index(i)); } void ConnectionModel::setPassword(const QString &name, const QString &password) { if (!m_connections.contains(name)) return; m_connections[name].password = password; const int i = indexOf(name); emit dataChanged(index(i), index(i)); } diff --git a/addons/katesql/connectionmodel.h b/addons/katesql/connectionmodel.h index da0010595..be7783fed 100644 --- a/addons/katesql/connectionmodel.h +++ b/addons/katesql/connectionmodel.h @@ -1,58 +1,58 @@ /* Copyright (C) 2010 Marco Mentasti This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef CONNECTIONMODEL_H #define CONNECTIONMODEL_H #include "connection.h" #include #include #include #include class ConnectionModel : public QAbstractListModel { Q_OBJECT public: ConnectionModel(QObject *parent = nullptr); ~ConnectionModel() override; int rowCount ( const QModelIndex & parent = QModelIndex() ) const override; QVariant data ( const QModelIndex & index, int role = Qt::DisplayRole ) const override; - virtual int addConnection(Connection conn); + virtual int addConnection(const Connection &conn); virtual void removeConnection(const QString &name); Connection::Status status(const QString &name) const; void setStatus(const QString &name, const Connection::Status status); void setPassword(const QString &name, const QString &password); int indexOf(const QString &name); // virtual bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()); // virtual bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()); private: QHash m_connections; QHash m_icons; }; #endif // CONNECTIONMODEL_H diff --git a/kate/kateappadaptor.cpp b/kate/kateappadaptor.cpp index 42e178d53..c9805a5bd 100644 --- a/kate/kateappadaptor.cpp +++ b/kate/kateappadaptor.cpp @@ -1,130 +1,130 @@ /* This file is part of the KDE project Copyright (C) 2001 Christoph Cullmann This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License version 2 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "kateappadaptor.h" #include "kateapp.h" #include "katesessionmanager.h" #include "katedocmanager.h" #include "katemainwindow.h" #include "katedebug.h" #include KateAppAdaptor::KateAppAdaptor(KateApp *app) : QDBusAbstractAdaptor(app) , m_app(app) {} void KateAppAdaptor::activate() { KateMainWindow *win = m_app->activeKateMainWindow(); if (!win) { return; } // like QtSingleApplication win->setWindowState(win->windowState() & ~Qt::WindowMinimized); win->raise(); win->activateWindow(); } bool KateAppAdaptor::openUrl(const QString &url, const QString &encoding) { return m_app->openUrl(QUrl(url), encoding, false); } -bool KateAppAdaptor::openUrl(QString url, QString encoding, bool isTempFile) +bool KateAppAdaptor::openUrl(const QString &url, const QString &encoding, bool isTempFile) { qCDebug(LOG_KATE) << "openURL"; return m_app->openUrl(QUrl(url), encoding, isTempFile); } bool KateAppAdaptor::isOnActivity(const QString &activity) { return m_app->isOnActivity(activity); } //----------- QString KateAppAdaptor::tokenOpenUrl(const QString &url, const QString &encoding) { KTextEditor::Document *doc = m_app->openDocUrl(QUrl(url), encoding, false); if (!doc) { return QStringLiteral("ERROR"); } return QStringLiteral("%1").arg((qptrdiff)doc); } QString KateAppAdaptor::tokenOpenUrl(const QString &url, const QString &encoding, bool isTempFile) { qCDebug(LOG_KATE) << "openURL"; KTextEditor::Document *doc = m_app->openDocUrl(QUrl(url), encoding, isTempFile); if (!doc) { return QStringLiteral("ERROR"); } return QStringLiteral("%1").arg((qptrdiff)doc); } QString KateAppAdaptor::tokenOpenUrlAt(const QString &url, int line, int column, const QString &encoding, bool isTempFile) { qCDebug(LOG_KATE) << "openURLAt"; KTextEditor::Document *doc = m_app->openDocUrl(QUrl(url), encoding, isTempFile); if (!doc) { return QStringLiteral("ERROR"); } m_app->setCursor(line, column); return QStringLiteral("%1").arg((qptrdiff)doc); } //-------- bool KateAppAdaptor::setCursor(int line, int column) { return m_app->setCursor(line, column); } bool KateAppAdaptor::openInput(const QString &text, const QString &encoding) { return m_app->openInput(text, encoding); } bool KateAppAdaptor::activateSession(const QString &session) { return m_app->sessionManager()->activateSession(session); } int KateAppAdaptor::desktopNumber() { KWindowInfo appInfo(m_app->activeKateMainWindow()->winId(), NET::WMDesktop); return appInfo.desktop(); } QString KateAppAdaptor::activeSession() { return m_app->sessionManager()->activeSession()->name(); } void KateAppAdaptor::emitExiting() { emit exiting(); } void KateAppAdaptor::emitDocumentClosed(const QString &token) { documentClosed(token); } diff --git a/kate/kateappadaptor.h b/kate/kateappadaptor.h index 7c3c69764..361b55118 100644 --- a/kate/kateappadaptor.h +++ b/kate/kateappadaptor.h @@ -1,125 +1,125 @@ /* This file is part of the KDE project Copyright (C) 2001 Christoph Cullmann This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef _kateapp_adaptor_h_ #define _kateapp_adaptor_h_ #include class KateApp; class KateAppAdaptor : public QDBusAbstractAdaptor { Q_OBJECT Q_CLASSINFO("D-Bus Interface", "org.kde.Kate.Application") Q_PROPERTY(QString activeSession READ activeSession) public: KateAppAdaptor(KateApp *app); /** * emit the exiting signal */ void emitExiting(); void emitDocumentClosed(const QString &token); public Q_SLOTS: /** * open a file with given url and encoding * will get view created * @param url url of the file * @param encoding encoding name * @return success */ bool openUrl(const QString &url, const QString &encoding); /** * checks if the Kate instance is in the specified activity * @param activity activity to check * @return true if it is in the specified activity, false otherwise */ bool isOnActivity(const QString &activity); /** * open a file with given url and encoding * will get view created * @param url url of the file * @param encoding encoding name * @return token or ERROR */ QString tokenOpenUrl(const QString &url, const QString &encoding); /** * Like the above, but adds an option to let the documentManager know * if the file should be deleted when closed. * @p isTempFile should be set to true with the --tempfile option set ONLY, * files opened with this set to true will be deleted when closed. */ - bool openUrl(QString url, QString encoding, bool isTempFile); + bool openUrl(const QString &url, const QString &encoding, bool isTempFile); QString tokenOpenUrl(const QString &url, const QString &encoding, bool isTempFile); QString tokenOpenUrlAt(const QString &url, int line, int column, const QString &encoding, bool isTempFile); /** * set cursor of active view in active main window * will clear selection * @param line line for cursor * @param column column for cursor * @return success */ bool setCursor(int line, int column); /** * helper to handle stdin input * open a new document/view, fill it with the text given * @param text text to fill in the new doc/view * @param encoding encoding to set for the document, if any * @return success */ bool openInput(const QString &text, const QString &encoding); /** * activate a given session * @param session session name * @return success */ bool activateSession(const QString &session); int desktopNumber(); /** * activate this kate instance */ void activate(); Q_SIGNALS: /** * Notify the world that this kate instance is exiting. * All apps should stop using the dbus interface of this instance after this signal got emitted. */ void exiting(); void documentClosed(const QString &token); public: QString activeSession(); private: KateApp *m_app; }; #endif