Files
@ 65c134a3d619
Branch filter:
Location: seniordesign-ui/GMap.NET.Core/GMap.NET.Internals/Cache.cs
65c134a3d619
7.9 KiB
text/x-csharp
Initial import of mapping source (huge commit)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
namespace GMap.NET.Internals
{
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using GMap.NET.CacheProviders;
internal class CacheLocator
{
private static string location;
public static string Location
{
get
{
if(string.IsNullOrEmpty(location))
{
Reset();
}
return location;
}
set
{
if(string.IsNullOrEmpty(value)) // setting to null resets to default
{
Reset();
}
else
{
location = value;
}
if(Delay)
{
Cache.Instance.CacheLocation = location;
}
}
}
static void Reset()
{
#if !PocketPC
location = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData) + Path.DirectorySeparatorChar + "GMap.NET" + Path.DirectorySeparatorChar;
// http://greatmaps.codeplex.com/discussions/403151
if(string.IsNullOrEmpty(location))
{
GMaps.Instance.Mode = AccessMode.ServerOnly;
GMaps.Instance.UseDirectionsCache = false;
GMaps.Instance.UseGeocoderCache = false;
GMaps.Instance.UsePlacemarkCache = false;
GMaps.Instance.UseRouteCache = false;
GMaps.Instance.UseUrlCache = false;
}
#else
location = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "GMap.NET" + Path.DirectorySeparatorChar;
#endif
}
public static bool Delay = false;
}
/// <summary>
/// cache system for tiles, geocoding, etc...
/// </summary>
internal class Cache : Singleton<Cache>
{
/// <summary>
/// abstract image cache
/// </summary>
public PureImageCache ImageCache;
/// <summary>
/// second level abstract image cache
/// </summary>
public PureImageCache ImageCacheSecond;
string cache;
/// <summary>
/// local cache location
/// </summary>
public string CacheLocation
{
get
{
return cache;
}
set
{
cache = value;
#if SQLite
if(ImageCache is SQLitePureImageCache)
{
(ImageCache as SQLitePureImageCache).CacheLocation = value;
}
#else
if(ImageCache is MsSQLCePureImageCache)
{
(ImageCache as MsSQLCePureImageCache).CacheLocation = value;
}
#endif
CacheLocator.Delay = true;
}
}
public Cache()
{
#region singleton check
if(Instance != null)
{
throw (new System.Exception("You have tried to create a new singleton class where you should have instanced it. Replace your \"new class()\" with \"class.Instance\""));
}
#endregion
#if SQLite
ImageCache = new SQLitePureImageCache();
#else
// you can use $ms stuff if you like too ;}
ImageCache = new MsSQLCePureImageCache();
#endif
#if PocketPC
// use sd card if exist for cache
string sd = Native.GetRemovableStorageDirectory();
if(!string.IsNullOrEmpty(sd))
{
CacheLocation = sd + Path.DirectorySeparatorChar + "GMap.NET" + Path.DirectorySeparatorChar;
}
else
#endif
{
#if PocketPC
CacheLocation = CacheLocator.Location;
#else
string newCache = CacheLocator.Location;
string oldCache = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "GMap.NET" + Path.DirectorySeparatorChar;
// move database to non-roaming user directory
if(Directory.Exists(oldCache))
{
try
{
if(Directory.Exists(newCache))
{
Directory.Delete(oldCache, true);
}
else
{
Directory.Move(oldCache, newCache);
}
CacheLocation = newCache;
}
catch(Exception ex)
{
CacheLocation = oldCache;
Trace.WriteLine("SQLitePureImageCache, moving data: " + ex.ToString());
}
}
else
{
CacheLocation = newCache;
}
#endif
}
}
#region -- etc cache --
public void SaveContent(string urlEnd, CacheType type, string content)
{
try
{
Stuff.RemoveInvalidPathSymbols(ref urlEnd);
string dir = Path.Combine(cache, type.ToString()) + Path.DirectorySeparatorChar;
// precrete dir
if(!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
string file = dir + urlEnd;
switch(type)
{
case CacheType.GeocoderCache:
file += ".geo";
break;
case CacheType.PlacemarkCache:
file += ".plc";
break;
case CacheType.RouteCache:
file += ".dragdir";
break;
case CacheType.UrlCache:
file += ".url";
break;
case CacheType.DirectionsCache:
file += ".dir";
break;
default:
file += ".txt";
break;
}
using(StreamWriter writer = new StreamWriter(file, false, Encoding.UTF8))
{
writer.Write(content);
}
}
catch(Exception ex)
{
Debug.WriteLine("SaveContent: " + ex);
}
}
public string GetContent(string urlEnd, CacheType type, TimeSpan stayInCache)
{
string ret = null;
try
{
Stuff.RemoveInvalidPathSymbols(ref urlEnd);
string dir = Path.Combine(cache, type.ToString()) + Path.DirectorySeparatorChar;
string file = dir + urlEnd;
switch(type)
{
case CacheType.GeocoderCache:
file += ".geo";
break;
case CacheType.PlacemarkCache:
file += ".plc";
break;
case CacheType.RouteCache:
file += ".dragdir";
break;
case CacheType.UrlCache:
file += ".url";
break;
default:
file += ".txt";
break;
}
if(File.Exists(file))
{
var writeTime = File.GetLastWriteTime(file);
if(DateTime.Now - writeTime < stayInCache)
{
using(StreamReader r = new StreamReader(file, Encoding.UTF8))
{
ret = r.ReadToEnd();
}
}
}
}
catch(Exception ex)
{
ret = null;
Debug.WriteLine("GetContent: " + ex);
}
return ret;
}
public string GetContent(string urlEnd, CacheType type)
{
return GetContent(urlEnd, type, TimeSpan.FromDays(88));
}
#endregion
}
internal enum CacheType
{
GeocoderCache,
PlacemarkCache,
RouteCache,
UrlCache,
DirectionsCache,
}
}
|