8
#include "backgroundthread.h"
16
#include <wx/filename.h>
17
#include "configmanager.h"
20
class LoaderBase : public AbstractJob
45
LoaderBase() : wait(true), data(0), len(0) {};
48
wxString FileName() const { return fileName; };
56
class FileLoader : public LoaderBase
59
FileLoader(const wxString& name) { fileName = name; };
65
* Delete a file after a grace period. This is useful since we do not know when the filesystem will sync its data to disk.
66
* In fact, the filesystem might be on a physically separate machine.
67
* Thus, whenever replacing an existing file with a new one, you would want to be sure the changes you made are really on disk
68
* before deleting any backup files (in case the user pulls the plug).
69
* The job does nothing but sleep (giving the OS an opportunity to flush caches), the actual work is done in the destructor.
70
* This enables you to feed the job to a BackgroundThread that owns its jobs (and gradually deletes them one by one).
71
* As the actual work is done in the destructor, no stale files will be left at application exit.
73
class DelayedDelete : public AbstractJob
77
DelayedDelete(const wxString& name) : target(name){};
83
if(Manager::IsAppShuttingDown()) // make sure we don't hang up the application for seconds
91
if(wxFile::Exists(target))
100
std::auto_ptr<char> ptr;
104
AutoBuffer() : ptr(0), len(0){};
105
AutoBuffer(size_t initial) : ptr(new char[initial]), len(initial){};
107
void Alloc(size_t size)
109
std::auto_ptr<char> tmp(new char[len + size]);
111
memcpy(tmp.get(), ptr.get(), len);
115
void Append(char *add_buf, size_t add_len)
118
memcpy(ptr.get() + len, add_buf, add_len);
122
size_t Length() const {return len;};
123
char *Data() const {return ptr.get();};
127
class URLLoader : public LoaderBase
131
URLLoader(const wxString& name) { fileName = name; };
136
class NullLoader : public LoaderBase
139
NullLoader(const wxString& name, char* buffer, size_t size) { fileName = name; data = buffer; len = size; Ready(); };
144
class FileManager : public Mgr<FileManager>
146
BackgroundThread fileLoaderThread;
147
BackgroundThread uncLoaderThread;
148
BackgroundThread urlLoaderThread;
149
BackgroundThread delayedDeleteThread;
151
FileManager() : fileLoaderThread(false), uncLoaderThread(false), urlLoaderThread(false){};
153
LoaderBase* Load(const wxString& file, bool reuseEditors = false);
155
bool Save(const wxString& file, const wxString& data, wxFontEncoding encoding, bool bom);
156
bool Save(const wxString& file, const char* data, size_t len);
158
bool ReplaceFile(const wxString& old_file, const wxString& new_file);