Minor updates for versions 0.2.1 and 0.2.2. See changelog file.
[movie-schedule] / src / searchclients / abstractsearchclient.cpp
index dc1219f..c19b7bc 100644 (file)
@@ -17,6 +17,8 @@
 
 #include "abstractsearchclient.h"
 
+#include "utils/timeutils.h"
+
 #include <QMutexLocker>
 #include <iostream>
 
@@ -27,7 +29,7 @@ AbstractSearchClient::AbstractSearchClient(QObject *parent)
     _start(0)
 {
     connect(_network, SIGNAL(finished(QNetworkReply *)),
-             this, SLOT(ReplyFinished(QNetworkReply*)));
+            this, SLOT(ReplyFinished(QNetworkReply*)));
     {
         QMutexLocker locker(&_next_search_task_id_mutex);
         _search_task_id = _next_search_task_id++;
@@ -83,6 +85,65 @@ void AbstractSearchClient::NetworkError(QNetworkReply::NetworkError error)
     deleteLater();
 }
 
+QList<QTime> AbstractSearchClient::TimesFromString(const QList<QString> &time_strings)
+{
+    QList<QTime> schedule_times;
+    bool am_pm = false;
+    Q_FOREACH(const QString time_str, time_strings) {
+        if (time_str.endsWith("am", Qt::CaseInsensitive) || time_str.endsWith("pm", Qt::CaseInsensitive)) {
+            am_pm = true;
+        }
+    }
+    if (am_pm) {
+        int i = 0;
+        bool am = true;
+        while (i < time_strings.length()) {
+            int j = i;
+            while (i < time_strings.length()) {
+                if (time_strings[i].endsWith("am", Qt::CaseInsensitive)) {
+                    am = true;
+                    break;
+                } else if (time_strings[i].endsWith("pm", Qt::CaseInsensitive)) {
+                    am = false;
+                    break;
+                }
+                ++i;
+            }
+            while (j < i) {
+                QString time_str = time_strings[j] + (am ? "am" : "pm");
+                QTime time = TimeUtils::FromTimeString(time_str);
+                if (time.isValid()) {
+                    schedule_times.append(time);
+                } else {
+                    std::cout << "ERROR: time " << qPrintable(time_str) << " is invalid." << std::endl;
+                }
+                ++j;
+            }
+            if (i < time_strings.length()) {
+                QString time_str = time_strings[i];
+                QTime time = TimeUtils::FromTimeString(time_str);
+                if (time.isValid()) {
+                    schedule_times.append(time);
+                } else {
+                    std::cout << "ERROR: time " << qPrintable(time_str) << " is invalid." << std::endl;
+                }
+                schedule_times.append(time);
+            }
+            ++i;
+        }
+    } else {
+        Q_FOREACH(const QString time_str, time_strings) {
+            QTime time = TimeUtils::FromTimeString(time_str);
+            if (time.isValid()) {
+                schedule_times.append(time);
+            } else {
+                std::cout << "ERROR: time " << qPrintable(time_str) << " is invalid." << std::endl;
+            }
+        }
+    }
+    return schedule_times;
+}
+
 void AbstractSearchClient::FixLocation(QUrl *url)
 {
     // Try to fix the Google url which returns