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