ec27fa0e38870b133480639a51921a2caa5f34af
[platform/upstream/syncevolution.git] / src / backends / file / FileSyncSource.h
1 /*
2  * Copyright (C) 2007 Patrick Ohly
3  * Copyright (C) 2007 Funambol
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #ifndef INCL_FILESYNCSOURCE
21 #define INCL_FILESYNCSOURCE
22
23 #include "TrackingSyncSource.h"
24
25 #ifdef ENABLE_FILE
26
27 #include <memory>
28 #include <boost/noncopyable.hpp>
29
30 /**
31  * Stores each SyncML item as a separate file in a directory.  The
32  * directory has to be specified via the database name, using
33  * [file://]<path> as format. The file:// prefix is optional, but the
34  * directory is only created if it is used.
35  * EvolutionSyncSource::getDatabaseID() gives us the database name.
36  *
37  * Change tracking is done via the file systems modification time
38  * stamp: editing a file treats it as modified and then sends it to
39  * the server in the next sync. Removing and adding files also works.
40  *
41  * The local unique identifier for each item is its name in the
42  * directory. New files are created using a running count which 
43  * initialized based on the initial content of the directory to
44  * "highest existing number + 1" and incremented to avoid collisions.
45  *
46  * Although this sync source itself does not care about the content of
47  * each item/file, the server needs to know what each item sent to it
48  * contains and what items the source is able to receive. Therefore
49  * the "type" property for this source must contain a data format
50  * specified, including a version for it. Here are some examples:
51  * - type=file:text/vcard:3.0
52  * - type=file:text/plain:1.0
53  */
54 class FileSyncSource : public TrackingSyncSource, private boost::noncopyable
55 {
56   public:
57     FileSyncSource(const EvolutionSyncSourceParams &params,
58                    const string &dataformat);
59
60
61  protected:
62     /* implementation of EvolutionSyncSource interface */
63     virtual void open();
64     virtual void close();
65     virtual Databases getDatabases();
66     virtual SyncItem *createItem(const string &uid);
67     virtual string fileSuffix() const;
68     virtual const char *getMimeType() const;
69     virtual const char *getMimeVersion() const;
70     virtual const char *getSupportedTypes() const;
71     virtual void logItem(const string &uid, const string &info, bool debug = false);
72     virtual void logItem(const SyncItem &item, const string &info, bool debug = false);
73
74     /* implementation of TrackingSyncSource interface */
75     virtual void listAllItems(RevisionMap_t &revisions);
76     virtual InsertItemResult insertItem(const string &uid, const SyncItem &item);
77     virtual void deleteItem(const string &uid);
78
79  private:
80     /**
81      * @name values obtained from the source's type property
82      *
83      * Other sync sources only support one hard-coded type and
84      * don't need such variables.
85      */
86     /**@{*/
87     string m_mimeType;
88     string m_mimeVersion;
89     string m_supportedTypes;
90     /**@}*/
91
92     /** directory selected via the database name in open(), reset in close() */
93     string m_basedir;
94     /** a counter which is used to name new files */
95     long m_entryCounter;
96
97     /**
98      * get access time for file, formatted as revision string
99      * @param filename    absolute path or path relative to current directory
100      */
101     string getATimeString(const string &filename);
102
103     /**
104      * create full filename from basedir and entry name
105      */
106     string createFilename(const string &entry);
107 };
108
109 #endif // ENABLE_FILE
110 #endif // INCL_FILESYNCSOURCE