added theme preview
[xcurtheme.git] / main.cpp
blob3c0258ee7bee0100e1964dc528d3c6eb88b42a14
1 /* coded by Ketmar // Vampire Avalon (ketmar@ketmar.no-ip.org)
3 * This program is free software. It comes without any warranty, to
4 * the extent permitted by applicable law. You can redistribute it
5 * and/or modify it under the terms of the Do What The Fuck You Want
6 * To Public License, Version 2, as published by Sam Hocevar. See
7 * http://sam.zoy.org/wtfpl/COPYING for more details.
8 */
9 #include <QtCore>
10 #include <QDebug>
12 #include "main.h"
14 #include <QApplication>
15 #include <QFile>
16 #include <QImage>
17 #include <QString>
18 #include <QStringList>
19 #include <QTextCodec>
20 #include <QTextStream>
23 #include "crtheme.h"
24 #include "previewwidget.h"
28 QString loadFile (const QString &fileName) {
29 QFile file(fileName);
30 QString res;
31 if (file.open(QIODevice::ReadOnly)) {
32 QTextStream stream;
33 stream.setDevice(&file);
34 stream.setCodec("UTF-8");
35 res = stream.readAll();
36 file.close();
38 return res;
43 static void doThemeScan (QStringList &lst, QDir &dir) {
44 QStringList filters; filters << "*";
45 QFileInfoList fl(dir.entryInfoList(filters, QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Readable | QDir::Executable));
46 foreach (const QFileInfo &fi, fl) {
47 if (dir.exists(fi.baseName()+"/cursors")) {
48 //qDebug() << fi.baseName();
49 lst << fi.baseName();
55 static QStringList enumThemes () {
56 QStringList res;
57 //qDebug() << QDir::homePath();
58 //QString pts("~/.icons:/usr/share/icons:/usr/share/pixmaps:/usr/X11R6/lib/X11/icons");
60 // get the search path from Xcursor
61 QString pts = XcursorLibraryPath();
62 //qDebug() << pts;
63 // separate the paths
64 QStringList pathList = pts.split(':', QString::SkipEmptyParts);
65 // remove duplicates
67 QMutableStringListIterator i(pathList);
68 while (i.hasNext()) {
69 const QString path = i.next();
70 QMutableStringListIterator j(i);
71 while (j.hasNext()) if (j.next() == path) j.remove();
74 // expand all occurrences of ~/ to the home dir
75 pathList.replaceInStrings(QRegExp("^~\\/"), QDir::home().path() + '/');
76 //qDebug() << pathList;
78 foreach (const QString &path, pathList) {
79 QDir d(path);
80 doThemeScan(res, d);
83 // remove duplicates
85 QMutableStringListIterator i(res);
86 while (i.hasNext()) {
87 const QString path = i.next();
88 QMutableStringListIterator j(i);
89 while (j.hasNext()) if (j.next() == path) j.remove();
93 return res;
97 ///////////////////////////////////////////////////////////////////////////////
98 int main (int argc, char *argv[]) {
99 QTextCodec::setCodecForCStrings(QTextCodec::codecForName("koi8-r"));
100 QTextCodec::setCodecForLocale(QTextCodec::codecForName("koi8-r"));
102 QApplication app(argc, argv);
104 QStringList tlist = enumThemes();
105 int f = 0;
106 foreach (const QString &s, tlist) {
107 qDebug() << f << "--" << s;
108 f++;
112 PreviewWidget *pw = new PreviewWidget;
113 XCursorTheme t(tlist[1]);
114 pw->setTheme(&t);
115 pw->show();
116 return app.exec();
119 if (argc < 2) return 0;
121 f = atoi(argv[1]);
122 qDebug() << f;
124 if (f >= 0 && f < tlist.size()) {
125 if (!haveXfixes()) {
126 fprintf(stderr, "ERROR! invalid Xfixes!\n");
127 return 1;
129 qDebug() << "loading theme:" << tlist[f];
130 XCursorTheme t(tlist[f]);
131 if (argc < 3) {
132 PreviewWidget *pw = new PreviewWidget;
133 pw->setTheme(t);
134 pw->show();
135 return app.exec();
136 } else applyTheme(t);
139 return 0;