Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 126 lines (100 sloc) 3.853 kb
d13aa57d »
2010-12-17 Initial version of QBrowsCap, with automatic update functionality, un…
1 #ifndef QBROWSCAP_H
2 #define QBROWSCAP_H
3
4
1020c5b5 »
2010-12-19 - Use of mutexes to ensure thread-safety instead of merely reentrancy.
5 #include <QMap>
d13aa57d »
2010-12-17 Initial version of QBrowsCap, with automatic update functionality, un…
6 #include <QStringList>
7 #include <QUrl>
8 #include <QNetworkAccessManager>
9 #include <QNetworkRequest>
10 #include <QNetworkReply>
11 #include <QObject>
12 #include <QFile>
13 #include <QFileInfo>
14 #include <QSqlDatabase>
15 #include <QSqlQuery>
16 #include <QSqlError>
17 #include <QEventLoop>
18 #include <QPair>
19 #include <QDateTime>
1020c5b5 »
2010-12-19 - Use of mutexes to ensure thread-safety instead of merely reentrancy.
20 #include <QMutex>
21 #include <QMutexLocker>
22 #include <QTextStream>
a80a59b6 »
2011-01-18 Add a QBrowsCapRecord class to return the results found by QBrowsCap,…
23 #include <QMetaType>
d13aa57d »
2010-12-17 Initial version of QBrowsCap, with automatic update functionality, un…
24 #include <QDebug>
25
26
d69554c6 »
2010-12-19 Restored the proper URLs for updating the browscap.csv file instead o…
27 #define QBROWSCAP_CSV_URL "http://browsers.garykeith.com/stream.asp?BrowsCapCSV"
28 #define QBROWSCAP_VERSION_URL "http://browsers.garykeith.com/versions/version-number.asp"
d13aa57d »
2010-12-17 Initial version of QBrowsCap, with automatic update functionality, un…
29 #define QBROWSCAP_INDEX_DB_VERSION_PATTERN "___QBROWSCAP_VERSION___"
30 #define QBROWSCAP_INDEX_DB_LAST_VERSION_PATTERN "___QBROWSCAP_LAST_VERSION___"
31 #define QBROWSCAP_INDEX_DB_LAST_VERSION_CHECK_PATTERN "___QBROWSCAP_LAST_VERSION_CHECK___"
32 #define QBROWSCAP_MIN_UPDATE_INTERVAL 86400 // Allow only daily updates.
33
34
a80a59b6 »
2011-01-18 Add a QBrowsCapRecord class to return the results found by QBrowsCap,…
35 struct QBrowsCapRecord {
36 QBrowsCapRecord() {}
37 ~QBrowsCapRecord() {}
38 QBrowsCapRecord(QString platform, QString browser_name,
39 QString browser_version, quint16 browser_version_major,
40 quint16 browser_version_minor, bool is_mobile)
41 {
42 this->platform = platform;
43 this->browser_name = browser_name;;
44 this->browser_version = browser_version;
45 this->browser_version_major = browser_version_major;
46 this->browser_version_minor = browser_version_minor;
47 this->is_mobile = is_mobile;
48 }
49
50 QString platform;
51 QString browser_name;
52 QString browser_version;
53 quint16 browser_version_major;
54 quint16 browser_version_minor;
55 bool is_mobile;
56 };
57
58 // Register metatype to allow these types to be streamed in QTests.
59 Q_DECLARE_METATYPE(QBrowsCapRecord)
60
61 #ifdef DEBUG
62 // QDebug() streaming output operators.
63 QDebug operator<<(QDebug dbg, const QBrowsCapRecord & record);
64 #endif
65
d13aa57d »
2010-12-17 Initial version of QBrowsCap, with automatic update functionality, un…
66 class QBrowsCap : public QObject {
67 Q_OBJECT
68
69 public:
70 QBrowsCap();
71 QBrowsCap(const QString & csvFile);
72 QBrowsCap(const QString & csvFile, const QString & indexFile);
73
74 void setCsvFile(const QString & csvFile);
75 void setIndexFile(const QString & indexFile);
76
77 bool selfUpdate();
78
79 int getCsvVersion() const;
80 int getLatestVersion();
81 int getIndexVersion() const;
82
1020c5b5 »
2010-12-19 - Use of mutexes to ensure thread-safety instead of merely reentrancy.
83 int getCacheSize() { QMutexLocker(&this->cacheMutex); return this->cache.size(); }
7d11f4ad »
2011-02-05 Added QBrowsCap::resetCache() to reset the in-memory cache.
84 void resetCache() { QMutexLocker(&this->cacheMutex); this->cache.clear(); }
1020c5b5 »
2010-12-19 - Use of mutexes to ensure thread-safety instead of merely reentrancy.
85
d13aa57d »
2010-12-17 Initial version of QBrowsCap, with automatic update functionality, un…
86 bool isUpToDate();
87 bool downloadUpdate(const QString & targetPath);
88 bool indexIsUpToDate() const;
89 bool buildIndex(bool force = false, bool ignoreCrawlers = true, bool ignorFeedReaders = true, bool ignoreBanned = true, bool ignoreNoJS = true);
90
a80a59b6 »
2011-01-18 Add a QBrowsCapRecord class to return the results found by QBrowsCap,…
91 QPair<bool, QBrowsCapRecord> matchUserAgent(const QString & userAgent);
d13aa57d »
2010-12-17 Initial version of QBrowsCap, with automatic update functionality, un…
92
93 protected slots:
94 void downloadFinished(QNetworkReply * reply);
95
96 signals:
97 void downloadedUpdate(bool ok, const QString & failureReason = QString::null);
98 void versionChecked(bool ok, int version, const QString & failureReason = QString::null);
99
100 protected:
101 // Download-related variables.
102 QNetworkAccessManager manager;
103 QString csvTargetPath;
104 bool csvDownloadResult, versionDownloadResult;
105 int latestVersion;
106
107 // The two speed-up layers: the index is persistent, the cache is not.
c9aa2bc0 »
2011-06-13 Implemented work-around for "QSqlDatabasePrivate::removeDatabase
108 //QSqlDatabase index;
a80a59b6 »
2011-01-18 Add a QBrowsCapRecord class to return the results found by QBrowsCap,…
109 QMap<QString, QPair<bool, QBrowsCapRecord> > cache;
d13aa57d »
2010-12-17 Initial version of QBrowsCap, with automatic update functionality, un…
110
111 // The browscap.csv file.
112 QString csvFile;
113
114 // The corresponding index (a SQLite DB).
115 QString indexFile;
116
a80a59b6 »
2011-01-18 Add a QBrowsCapRecord class to return the results found by QBrowsCap,…
117 // Mutexes are necessary to make the SQLite DB queries and the cache
1020c5b5 »
2010-12-19 - Use of mutexes to ensure thread-safety instead of merely reentrancy.
118 // lookups/insertions thread-safe (all calls are serialized).
119 QMutex queryMutex;
120 QMutex cacheMutex;
121
d13aa57d »
2010-12-17 Initial version of QBrowsCap, with automatic update functionality, un…
122 void init();
123 bool connectIndexDB();
124 };
125
126 #endif // QBROWSCAP_H
Something went wrong with that request. Please try again.