823092e1eeb0888c373d2e8e3fe8d0eafdca50b5
[platform/upstream/syncevolution.git] / src / backends / file / FileSyncSourceRegister.cpp
1 /*
2  * Copyright (C) 2008 Patrick Ohly
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "FileSyncSource.h"
20
21 static EvolutionSyncSource *createSource(const EvolutionSyncSourceParams &params)
22 {
23     pair <string, string> sourceType = EvolutionSyncSource::getSourceType(params.m_nodes);
24     // The string returned by getSourceType() is always the one
25     // registered as main Aliases() below.
26     bool isMe = sourceType.first == "Files in one directory";
27
28 #ifndef ENABLE_FILE
29     // tell SyncEvolution if the user wanted to use a disabled sync source,
30     // otherwise let it continue searching
31     return isMe ? RegisterSyncSource::InactiveSource : NULL;
32 #else
33     // Also recognize one of the standard types?
34     // Not in the FileSyncSource!
35     bool maybeMe = false /* sourceType.first == "addressbook" */;
36     
37     if (isMe || maybeMe) {
38         // The FileSyncSource always needs the data format
39         // parameter in sourceType.second.
40         if (/* sourceType.second == "" || sourceType.second == "text/x-vcard" */
41             sourceType.second.size()) {
42             return new FileSyncSource(params, sourceType.second);
43         } else {
44             return NULL;
45         }
46     }
47     return NULL;
48 #endif
49 }
50
51 static RegisterSyncSource registerMe("Files in one directory",
52 #ifdef ENABLE_FILE
53                                      true,
54 #else
55                                      false,
56 #endif
57                                      createSource,
58                                      "Files in one directory = file\n"
59                                      "   Stores items in one directory as one file per item.\n"
60                                      "   The directory is selected via [file://]<path>; it\n"
61                                      "   will only be created if the prefix is given, otherwise\n"
62                                      "   it must exist already. Only items of the same type can\n"
63                                      "   be synchronized and this type must be specified explicitly\n"
64                                      "   with both mime type and version.\n"
65                                      "   Examples:\n"
66                                      "      file:text/plain:1.0\n"
67                                      "      file:text/x-vcard:2.1\n"
68                                      "      file:text/vcard:3.0\n"
69                                      "      file:text/x-calendar:1.0\n"
70                                      "      file:text/calendar:2.0\n",
71                                      Values() +
72                                      (Aliases("Files in one directory") + "file"));
73
74 #ifdef ENABLE_FILE
75 #ifdef ENABLE_UNIT_TESTS
76
77 class FileSyncSourceUnitTest : public CppUnit::TestFixture {
78     CPPUNIT_TEST_SUITE(FileSyncSourceUnitTest);
79     CPPUNIT_TEST(testInstantiate);
80     CPPUNIT_TEST_SUITE_END();
81
82 protected:
83     void testInstantiate() {
84         boost::shared_ptr<EvolutionSyncSource> source;
85         source.reset(EvolutionSyncSource::createTestingSource("file", "file:text/vcard:3.0", true));
86         source.reset(EvolutionSyncSource::createTestingSource("file", "file:text/plain:1.0", true));
87         source.reset(EvolutionSyncSource::createTestingSource("file", "Files in one directory:text/x-vcard:2.1", true));
88     }
89 };
90
91 SYNCEVOLUTION_TEST_SUITE_REGISTRATION(FileSyncSourceUnitTest);
92
93 #endif // ENABLE_UNIT_TESTS
94
95 #ifdef ENABLE_INTEGRATION_TESTS
96
97 // The anonymous namespace ensures that we don't get
98 // name clashes: although the classes and objects are
99 // only defined in this file, the methods generated
100 // for local classes will clash with other methods
101 // of other classes with the same name if no namespace
102 // is used.
103 //
104 // The code above is not yet inside the anonymous
105 // name space because it would show up inside
106 // the CPPUnit unit test names. Use a unique class
107 // name there.
108 namespace {
109 #if 0
110 }
111 #endif
112
113 static class VCard21Test : public RegisterSyncSourceTest {
114 public:
115     VCard21Test() : RegisterSyncSourceTest("file_vcard21", "vcard21") {}
116
117     virtual void updateConfig(ClientTestConfig &config) const
118     {
119         // set type as required by FileSyncSource
120         // and leave everything else at its default
121         config.type = "file:text/x-vcard:2.1";
122     }
123 } VCard21Test;
124
125 static class VCard30Test : public RegisterSyncSourceTest {
126 public:
127     VCard30Test() : RegisterSyncSourceTest("file_vcard30", "vcard30") {}
128
129     virtual void updateConfig(ClientTestConfig &config) const
130     {
131         config.type = "file:text/vcard:3.0";
132     }
133 } VCard30Test;
134
135 static class ICal20Test : public RegisterSyncSourceTest {
136 public:
137     ICal20Test() : RegisterSyncSourceTest("file_ical20", "ical20") {}
138
139     virtual void updateConfig(ClientTestConfig &config) const
140     {
141         config.type = "file:text/calendar:2.0";
142
143         // A sync source which supports linked items (= recurring
144         // event with detached exception) is expected to handle
145         // inserting the parent or child twice by turning the
146         // second operation into an update. The file backend is
147         // to dumb for that and therefore fails these tests:
148         //
149         // Client::Source::file_ical20::testLinkedItemsInsertParentTwice
150         // Client::Source::file_ical20::testLinkedItemsInsertChildTwice
151         //
152         // Disable linked item testing to avoid this.
153         config.parentItem =
154             config.childItem = NULL;
155     }
156 } ICal20Test;
157
158 static class ITodo20Test : public RegisterSyncSourceTest {
159 public:
160     ITodo20Test() : RegisterSyncSourceTest("file_itodo20", "itodo20") {}
161
162     virtual void updateConfig(ClientTestConfig &config) const
163     {
164         config.type = "file:text/calendar:2.0";
165     }
166 } ITodo20Test;
167
168 }
169 #endif // ENABLE_INTEGRATION_TESTS
170
171 #endif // ENABLE_FILE