1 /* This file is part of the KDE project
2    Copyright (C) 2002 Werner Trobin <trobin@kde.org>
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this library; see the file COPYING.LIB.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 #include <QFile>
21 #include <QByteArray>
22 #include <QList>
23 #include <QPluginLoader>
24 
25 #include "KoDocumentEntry.h"
26 #include "KoFilterEntry.h"
27 #include "KoFilterManager.h"
28 
29 #include <MainDebug.h>
30 
main(int,char **)31 int main(int /*argc*/, char ** /*argv*/)
32 {
33     QByteArray output = "digraph filters {\n";
34 
35     // The following code is shamelessly copied over from Calligra::Graph::buildGraph
36     // It wasn't feasible to do some serious changes in the lib for that tiny bit
37     // of duplicated code in a test file.
38 
39     QList<QString> vertices; // to keep track of already inserted values, not performance critical
40 
41     // Make sure that all available parts are added to the graph
42     const QList<KoDocumentEntry> parts(KoDocumentEntry::query());
43     QList<KoDocumentEntry>::ConstIterator partIt(parts.begin());
44     QList<KoDocumentEntry>::ConstIterator partEnd(parts.end());
45 
46     while (partIt != partEnd) {
47         //kDebug() << ( *partIt ).service()->desktopEntryName();
48         QJsonObject metaData = (*partIt).metaData();
49         QStringList nativeMimeTypes = metaData.value("X-KDE-ExtraNativeMimeTypes").toString().split(',');
50         nativeMimeTypes += metaData.value("X-KDE-NativeMimeType").toString();
51         QStringList::ConstIterator it = nativeMimeTypes.constBegin();
52         QStringList::ConstIterator end = nativeMimeTypes.constEnd();
53         for (; it != end; ++it) {
54             QString key = *it;
55             //kDebug() <<"" << key;
56             if (!key.isEmpty()) {
57                 output += "    \"";
58                 output += key.toLatin1();
59                 output += "\" [shape=box, style=filled, fillcolor=lightblue];\n";
60                 if (! vertices.contains(key))
61                     vertices.append(key);
62             }
63         }
64         ++partIt;
65     }
66 
67     const QList<KoFilterEntry::Ptr> filters(KoFilterEntry::query());   // no constraint here - we want *all* :)
68     QList<KoFilterEntry::Ptr>::ConstIterator it = filters.begin();
69     QList<KoFilterEntry::Ptr>::ConstIterator end = filters.end();
70 
71     for (; it != end; ++it) {
72         qDebug() << "import" << (*it)->import << " export" << (*it)->export_;
73         // First add the "starting points"
74         QStringList::ConstIterator importIt = (*it)->import.constBegin();
75         QStringList::ConstIterator importEnd = (*it)->import.constEnd();
76         for (; importIt != importEnd; ++importIt) {
77             // already there?
78             if (! vertices.contains(*importIt)) {
79                 vertices.append(*importIt);
80                 output += "    \"";
81                 output += (*importIt).toLatin1();
82                 output += "\";\n";
83             }
84         }
85 
86         QStringList::ConstIterator exportIt = (*it)->export_.constBegin();
87         QStringList::ConstIterator exportEnd = (*it)->export_.constEnd();
88 
89         for (; exportIt != exportEnd; ++exportIt) {
90             // First make sure the export vertex is in place
91             if (! vertices.contains(*exportIt)) {
92                 output += "    \"";
93                 output += (*exportIt).toLatin1();
94                 output += "\";\n";
95                 vertices.append(*exportIt);
96             }
97             // Then create the appropriate edges
98             importIt = (*it)->import.constBegin();
99             for (; importIt != importEnd; ++importIt) {
100                 output += "    \"";
101                 output += (*importIt).toLatin1();
102                 output += "\" -> \"";
103                 output += (*exportIt).toLatin1();
104                 if (KoFilterManager::filterAvailable(*it))
105                     output += "\";\n";
106                 else
107                     output += "\" [style=dotted];\n";
108             }
109         }
110     }
111 
112     output += "}\n";
113 
114     QFile f("graph.dot");
115     if (f.open(QIODevice::WriteOnly))
116         f.write(output);
117     f.close();
118     return 0;
119 }
120