Get image paths from tracker
authorTimo Härkönen <timop.harkonen@gmail.com>
Tue, 28 Sep 2010 16:17:44 +0000 (19:17 +0300)
committerTimo Härkönen <timop.harkonen@gmail.com>
Tue, 28 Sep 2010 16:17:44 +0000 (19:17 +0300)
src/settings.cpp
src/settings.h
src/settingsdialog.cpp
src/settingsdialog.h
src/src.pro
src/statistics.cpp
src/statistics.h
src/statisticsdialog.cpp
src/statisticsdialog.h
src/trackerfiles.cpp [new file with mode: 0644]
src/trackerfiles.h [new file with mode: 0644]

index 3a418be..151c5f5 100644 (file)
@@ -67,3 +67,13 @@ void Settings::setImagePath(const QString &path)
 {
     imagePath_ = path;
 }
+
+QStringList Settings::localImages() const
+{
+    return imageList_;
+}
+
+void Settings::setLocalImages(const QStringList &files)
+{
+    imageList_ = files;
+}
index bb45e67..4a5a6bc 100644 (file)
@@ -38,6 +38,9 @@ public:
     QString imagePath() const;
     void setImagePath(const QString &path);
 
+    QStringList localImages() const;
+    void setLocalImages(const QStringList &files);
+
 private:
     Settings(QObject *parent = 0);
 
@@ -46,5 +49,6 @@ private:
     int pieceCount_;
     QPixmap image_;
     QString imagePath_;
+    QStringList imageList_;
 };
 #endif
index 0f2a6cb..86834f0 100644 (file)
@@ -19,6 +19,7 @@
 #include "settingsdialog.h"
 #include "settings.h"
 #include "defines.h"
+#include "trackerfiles.h"
 
 #include <QRadioButton>
 #include <QPushButton>
@@ -67,12 +68,15 @@ SettingsDialog::SettingsDialog(QWidget *parent) :
 
     selectedImageLabel_->setVisible(false);
 
+    trackerFiles_ = new TrackerFiles(this);
+
     setLayout(mainLayout_);
 
     connect(easyButton_, SIGNAL(toggled(bool)), this, SLOT(difficultySelectionChanged(bool)));
     //connect(imageCombo_, SIGNAL(currentIndexChanged(QString)), this, SLOT(imageSelectionChanged(QString)));
     connect(imageCombo_, SIGNAL(activated(QString)), this, SLOT(imageSelectionChanged(QString)));
     connect(applyButton_, SIGNAL(clicked()), this, SLOT(applyClicked()));
+    connect(trackerFiles_, SIGNAL(filesRead(QStringList)), this, SLOT(trackerFilesRead(QStringList)));
 }
 
 int SettingsDialog::exec()
@@ -98,32 +102,11 @@ void SettingsDialog::difficultySelectionChanged(bool value)
 void SettingsDialog::imageSelectionChanged(const QString &txt)
 {
     if(txt == RANDOM_IMAGE_TXT) {
-        qDebug() << "Random image selected";
-
-        // Get random image from ~/MyDocs/.images/
-        //TODO: images from other directories
-        QStringList filters;
-        filters << "*.jpg" << "*.png" << "*.xpm";
-
-        QDir dir(QDir::homePath() + QLatin1String("/MyDocs/.images"));
-        //dir.setNameFilters(filters);
-
-        QStringList pics = dir.entryList(filters, QDir::Files | QDir::NoSymLinks);
-
-        //qDebug() << QString("pics list contains %1 entries").arg(pics.count());
-
-        if(!pics.isEmpty()) {
-            QString path = QDir::homePath() + QLatin1String("/MyDocs/.images/") + pics.at(qrand() % pics.count());
-            Settings::instance()->setImage(QPixmap(path));
-            Settings::instance()->setImagePath(path);
+        if(Settings::instance()->localImages().isEmpty()) {
+            trackerFiles_->readFiles();
         }
         else {
-            Settings::instance()->setImage(QPixmap(":/images/default.jpg"));
-            Settings::instance()->setImagePath("default");
-        }
-
-        if(selectedImageLabel_->isVisible()) {
-            selectedImageLabel_->setVisible(false);
+            trackerFilesRead(Settings::instance()->localImages());
         }
     }
     else if(txt == SELECT_IMAGE_TXT) {
@@ -159,3 +142,25 @@ void SettingsDialog::applyClicked()
 {
     this->done(1);
 }
+
+void SettingsDialog::trackerFilesRead(const QStringList &files)
+{
+    if(!files.isEmpty()) {
+        QString path = files.at(qrand() % files.count());
+        path = path.trimmed();
+        Settings::instance()->setImage(QPixmap(path));
+        Settings::instance()->setImagePath(path);
+
+        if(Settings::instance()->localImages().isEmpty()) {
+            Settings::instance()->setLocalImages(files);
+        }
+    }
+    else {
+        Settings::instance()->setImage(QPixmap(":/images/default.jpg"));
+        Settings::instance()->setImagePath("default");
+    }
+
+    if(selectedImageLabel_->isVisible()) {
+        selectedImageLabel_->setVisible(false);
+    }
+}
index 23b82ea..70cf8c9 100644 (file)
@@ -28,6 +28,7 @@ class QHBoxLayout;
 class QComboBox;
 class QLabel;
 class QPushButton;
+class TrackerFiles;
 
 class SettingsDialog : public QDialog
 {
@@ -43,6 +44,7 @@ private slots:
     void difficultySelectionChanged(bool value);
     void imageSelectionChanged(const QString &txt);
     void applyClicked();
+    void trackerFilesRead(const QStringList &files);
 
 private:
     QVBoxLayout *mainLayout_;
@@ -58,5 +60,7 @@ private:
     QLabel *selectedImageLabel_;
 
     QPushButton *applyButton_;
+
+    TrackerFiles *trackerFiles_;
 };
 #endif
index 2b28d76..92b6ee4 100644 (file)
@@ -16,7 +16,8 @@ HEADERS += gameview.h \
     settingsdialog.h \
     aboutdialog.h \
     statistics.h \
-    statisticsdialog.h
+    statisticsdialog.h \
+    trackerfiles.h
 SOURCES += gameview.cpp \
     main.cpp \
     mainwindow.cpp \
@@ -27,7 +28,8 @@ SOURCES += gameview.cpp \
     settingsdialog.cpp \
     aboutdialog.cpp \
     statistics.cpp \
-    statisticsdialog.cpp
+    statisticsdialog.cpp \
+    trackerfiles.cpp
 RESOURCES += resources.qrc
 desktop.files += impuzzle.desktop
 desktop.path = /usr/share/applications/hildon/
index c979da0..b81af23 100644 (file)
@@ -1,3 +1,21 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include "statistics.h"
 #include "defines.h"
 
index 8b2733c..67c6370 100644 (file)
@@ -1,3 +1,21 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #ifndef STATISTICS_H
 #define STATISTICS_H
 
index 28ed7ba..c8915f9 100644 (file)
@@ -1,3 +1,21 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #include "statisticsdialog.h"
 #include "statistics.h"
 
index cabecdd..b2faf80 100644 (file)
@@ -1,3 +1,21 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
 #ifndef STATISTICSDIALOG_H
 #define STATISTICSDIALOG_H
 
diff --git a/src/trackerfiles.cpp b/src/trackerfiles.cpp
new file mode 100644 (file)
index 0000000..1890500
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "trackerfiles.h"
+
+#include <QDebug>
+
+TrackerFiles::TrackerFiles(QObject *parent) :
+    QObject(parent)
+{
+    tracker_ = new QProcess(this);
+
+    connect(tracker_, SIGNAL(finished(int,QProcess::ExitStatus)),
+            this, SLOT(processFinished(int,QProcess::ExitStatus)));
+}
+
+void TrackerFiles::readFiles()
+{
+    QString program("tracker-files");
+    QStringList args;
+    args << "-m" << "image/png" << "-m" << "image/jpeg";
+    tracker_->start(program, args);
+    // FIXME: this blocks execution
+    tracker_->waitForFinished();
+}
+
+void TrackerFiles::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
+{
+    QStringList files;
+
+    if(exitCode == 0 && exitStatus == QProcess::NormalExit) {
+        QString stdOut = tracker_->readAllStandardOutput();
+        files = stdOut.split("\n");
+        // first row is not a path
+        if(!files.isEmpty()) {
+            files.removeAt(0);
+        }
+
+        files.removeAll("");
+        qDebug() << QString("Found %1 images").arg(files.count());
+    }
+    else {
+        qDebug() << tracker_->errorString();
+    }
+
+    emit filesRead(files);
+}
diff --git a/src/trackerfiles.h b/src/trackerfiles.h
new file mode 100644 (file)
index 0000000..9e353b4
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+  Image Puzzle - A set your pieces straight game
+  Copyright (C) 2009  Timo Härkönen
+
+  This program is free software: you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation, either version 3 of the License, or
+  (at your option) any later version.
+
+  This program 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 General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TRACKERFILES_H
+#define TRACKERFILES_H
+
+#include <QObject>
+#include <QProcess>
+
+class TrackerFiles : public QObject
+{
+Q_OBJECT
+public:
+    explicit TrackerFiles(QObject *parent = 0);
+
+signals:
+    void filesRead(const QStringList &files);
+
+public slots:
+    void readFiles();
+
+private slots:
+    void processFinished(int exitCode, QProcess::ExitStatus exitStatus);
+
+private:
+    QProcess *tracker_;
+};
+
+#endif // TRACKERFILES_H