dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
cache.cpp
Go to the documentation of this file.
1 
13 /*
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, see <http://www.gnu.org/licenses/>
26 */
27 #include "cache.h"
28 #include "utils/pathutils.h"
29 #include <QSettings>
30 
31 namespace core {
32  Cache* Cache::m_pInstance=nullptr;
33 
35  {
36  if(!m_pInstance)
37  m_pInstance=new Cache;
38  return m_pInstance;
39  }
40 
41  void Cache::setCacheLocation(const QString& value)
42  {
43  cache=value;
44  routeCache = cache + "RouteCache" + QDir::separator();
45  geoCache = cache + "GeocoderCache"+ QDir::separator();
46  placemarkCache = cache + "PlacemarkCache" + QDir::separator();
48  }
50  {
51  return cache;
52  }
53  Cache::Cache()
54  {
55  if(cache.isNull()|cache.isEmpty())
56  {
57  cache= Utils::PathUtils().GetStoragePath()+"mapscache"+QDir::separator();
58  setCacheLocation(cache);
59  }
60  }
61  QString Cache::GetGeocoderFromCache(const QString &urlEnd)
62  {
63 #ifdef DEBUG_GetGeocoderFromCache
64  qDebug()<<"Entered GetGeocoderFromCache";
65 #endif
66  QString ret;
67  QString filename=geoCache+QString(urlEnd)+".geo";
68 #ifdef DEBUG_GetGeocoderFromCache
69  qDebug()<<"GetGeocoderFromCache: Does file exist?:"<<filename;
70 #endif
71  QFileInfo File(filename);
72  if (File .exists())
73  {
74 #ifdef DEBUG_GetGeocoderFromCache
75  qDebug()<<"GetGeocoderFromCache:File exists!!";
76 #endif
77  QFile file(filename);
78  if (file.open(QIODevice::ReadOnly))
79  {
80  QTextStream stream(&file);
81  stream.setCodec("UTF-8");
82  ret = stream.readAll();
83  }
84  }
85 #ifdef DEBUG_GetGeocoderFromCache
86  qDebug()<<"GetGeocoderFromCache:Returning:"<<ret;
87 #endif
88  return ret;
89  }
90  void Cache::CacheGeocoder(const QString &urlEnd, const QString &content)
91  {
92  QString ret;
93  QString filename=geoCache+QString(urlEnd)+".geo";
94 #ifdef DEBUG_CACHE
95  qDebug()<<"CacheGeocoder: Filename:"<<filename;
96 #endif //DEBUG_CACHE
97  QFileInfo File(filename);;
98  QDir dir=File.absoluteDir();
99  QString path=dir.absolutePath();
100 #ifdef DEBUG_CACHE
101  qDebug()<<"CacheGeocoder: Path:"<<path;
102 #endif //DEBUG_CACHE
103  if(!dir.exists())
104  {
105 #ifdef DEBUG_CACHE
106  qDebug()<<"CacheGeocoder: Cache path doesn't exist, try to create";
107 #endif //DEBUG_CACHE
108  if(!dir.mkpath(path))
109  {
110 #ifdef DEBUG_CACHE
111  qDebug()<<"GetGeocoderFromCache: Could not create path";
112 #endif //DEBUG_CACHE
113  }
114  }
115 #ifdef DEBUG_CACHE
116  qDebug()<<"CacheGeocoder: OpenFile:"<<filename;
117 #endif //DEBUG_CACHE
118  QFile file(filename);
119  if (file.open(QIODevice::WriteOnly))
120  {
121 #ifdef DEBUG_CACHE
122  qDebug()<<"CacheGeocoder: File Opened!!!:"<<filename;
123 #endif //DEBUG_CACHE
124  QTextStream stream(&file);
125  stream.setCodec("UTF-8");
126  stream<<content;
127  }
128  }
129  QString Cache::GetPlacemarkFromCache(const QString &urlEnd)
130  {
131 #ifdef DEBUG_CACHE
132  qDebug()<<"Entered GetPlacemarkFromCache";
133 #endif //DEBUG_CACHE
134  QString ret;
135  QString filename=placemarkCache+QString(urlEnd)+".plc";
136 #ifdef DEBUG_CACHE
137  qDebug()<<"GetPlacemarkFromCache: Does file exist?:"<<filename;
138 #endif //DEBUG_CACHE
139  QFileInfo File(filename);
140  if (File .exists())
141  {
142 #ifdef DEBUG_CACHE
143  qDebug()<<"GetPlacemarkFromCache:File exists!!";
144 #endif //DEBUG_CACHE
145  QFile file(filename);
146  if (file.open(QIODevice::ReadOnly))
147  {
148  QTextStream stream(&file);
149  stream.setCodec("UTF-8");
150  ret = stream.readAll();
151  }
152  }
153 #ifdef DEBUG_CACHE
154  qDebug()<<"GetPlacemarkFromCache:Returning:"<<ret;
155 #endif //DEBUG_CACHE
156  return ret;
157  }
158  void Cache::CachePlacemark(const QString &urlEnd, const QString &content)
159  {
160  QString ret;
161  QString filename=placemarkCache+QString(urlEnd)+".plc";
162 #ifdef DEBUG_CACHE
163  qDebug()<<"CachePlacemark: Filename:"<<filename;
164 #endif //DEBUG_CACHE
165  QFileInfo File(filename);;
166  QDir dir=File.absoluteDir();
167  QString path=dir.absolutePath();
168 #ifdef DEBUG_CACHE
169  qDebug()<<"CachePlacemark: Path:"<<path;
170 #endif //DEBUG_CACHE
171  if(!dir.exists())
172  {
173 #ifdef DEBUG_CACHE
174  qDebug()<<"CachePlacemark: Cache path doesn't exist, try to create";
175 #endif //DEBUG_CACHE
176  if(!dir.mkpath(path))
177  {
178 #ifdef DEBUG_CACHE
179  qDebug()<<"CachePlacemark: Could not create path";
180 #endif //DEBUG_CACHE
181  }
182  }
183 #ifdef DEBUG_CACHE
184  qDebug()<<"CachePlacemark: OpenFile:"<<filename;
185 #endif //DEBUG_CACHE
186  QFile file(filename);
187  if (file.open(QIODevice::WriteOnly))
188  {
189 #ifdef DEBUG_CACHE
190  qDebug()<<"CachePlacemark: File Opened!!!:"<<filename;
191 #endif //DEBUG_CACHE
192  QTextStream stream(&file);
193  stream.setCodec("UTF-8");
194  stream<<content;
195  }
196  }
197 }
static Cache * Instance()
Definition: cache.cpp:34
QString GetGeocoderFromCache(const QString &urlEnd)
Definition: cache.cpp:61
Parse log file
void setGtileCache(const QString &value)
void CachePlacemark(const QString &urlEnd, const QString &content)
Definition: cache.cpp:158
QString CacheLocation()
Definition: cache.cpp:49
QString GetStoragePath()
Definition: pathutils.cpp:98
void setCacheLocation(const QString &value)
Definition: cache.cpp:41
QString GetPlacemarkFromCache(const QString &urlEnd)
Definition: cache.cpp:129
PureImageCache ImageCache
Definition: cache.h:41
void CacheGeocoder(const QString &urlEnd, const QString &content)
Definition: cache.cpp:90