OSDN Git Service

Support password timeout.
[tombo/Tombo.git] / Src / Repository.cpp
1 #include <windows.h>\r
2 #include <tchar.h>\r
3 \r
4 #include "Tombo.h"\r
5 #include "message.h"\r
6 #include "TString.h"\r
7 #include "Uniconv.h"\r
8 #include "TomboURI.h"\r
9 #include "Repository.h"\r
10 #include "Property.h"\r
11 \r
12 #include "AutoPtr.h"\r
13 \r
14 #include "RepositoryImpl.h"\r
15 \r
16 // At this time, Repository is only proxy to RepositoryImpl.\r
17 // This class will choose repository implementations in future.\r
18 \r
19 /////////////////////////////////////////\r
20 // Global definition\r
21 /////////////////////////////////////////\r
22 \r
23 Repository g_Repository;\r
24 \r
25 /////////////////////////////////////////\r
26 // Repository ctor & dtor, initializer\r
27 /////////////////////////////////////////\r
28 \r
29 Repository::Repository() : pDefaultImpl(NULL)\r
30 {\r
31 }\r
32 \r
33 Repository::~Repository()\r
34 {\r
35 }\r
36 \r
37 BOOL Repository::Init()\r
38 {\r
39         if (!vSubRepository.Init(10, 10)) return FALSE;\r
40         return ClearSubRepository();\r
41 }\r
42 \r
43 BOOL Repository::ClearSubRepository()\r
44 {\r
45         for (DWORD i = 0; i < vSubRepository.NumItems(); i++) {\r
46                 RepositoryImpl *p = *vSubRepository.GetUnit(i);\r
47                 delete p;\r
48         }\r
49         vSubRepository.Clear(FALSE);\r
50 \r
51         pDefaultImpl = NULL;\r
52         return TRUE;\r
53 }\r
54 \r
55 BOOL Repository::AddSubRepository(RepositoryImpl *pImpl)\r
56 {\r
57         if (!vSubRepository.Add(&pImpl)) return FALSE;\r
58 \r
59         pDefaultImpl = *vSubRepository.GetUnit(0);\r
60         return TRUE;\r
61 }\r
62 \r
63 BOOL Repository::GetAttachURI(const TomboURI *pBase, TomboURI *pAttached)\r
64 {\r
65         if (pBase->IsLeaf()) {\r
66                 if (!pBase->GetParent(pAttached)) return FALSE;\r
67         } else {\r
68                 if (!pAttached->Init(pBase->GetFullURI())) return FALSE;\r
69         }\r
70         return TRUE;\r
71 }\r
72 \r
73 RepositoryImpl *Repository::GetAssocSubRepository(const TomboURI *pURI)\r
74 {\r
75         DWORD n = vSubRepository.NumItems();\r
76         for (DWORD i = 0; i < n; i++) {\r
77                 RepositoryImpl *pImpl = *vSubRepository.GetUnit(i);\r
78                 if (_tcsncmp(pImpl->GetRootURI()->GetFullURI() + 8, pURI->GetFullURI() + 8, pImpl->GetRepNameLen() + 1) == 0) {\r
79                         return pImpl;\r
80                 }\r
81         }\r
82         return NULL;\r
83 }\r
84 \r
85 /////////////////////////////////////////\r
86 // delegated methods\r
87 /////////////////////////////////////////\r
88 \r
89 BOOL Repository::Update(const TomboURI *pCurrentURI, \r
90                                                 LPCTSTR pData, \r
91                                                 TomboURI *pNewURI, TString *pNewHeadLine)\r
92 {\r
93         // pNewURI is in same sub repository\r
94         RepositoryImpl *pImpl = GetAssocSubRepository(pCurrentURI);\r
95         if (pImpl == NULL) return FALSE;\r
96         return pImpl->Update(pCurrentURI, pData, pNewURI, pNewHeadLine);\r
97 }\r
98 \r
99 BOOL Repository::GetHeadLine(const TomboURI *pURI, TString *pHeadLine)\r
100 {\r
101         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
102         if (pImpl == NULL) return FALSE;\r
103         return pImpl->GetHeadLine(pURI, pHeadLine);\r
104 }\r
105 \r
106 BOOL Repository::GetOption(const TomboURI *pURI, URIOption *pOption)\r
107 {\r
108         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
109         if (pImpl == NULL) return FALSE;\r
110         return pImpl->GetOption(pURI, pOption);\r
111 }\r
112 \r
113 BOOL Repository::SetOption(const TomboURI *pCurrentURI, URIOption *pOption)\r
114 {\r
115         RepositoryImpl *pImpl = GetAssocSubRepository(pCurrentURI);\r
116         if (pImpl == NULL) return FALSE;\r
117         return pImpl->SetOption(pCurrentURI, pOption);\r
118 }\r
119 \r
120 BOOL Repository::IsEncrypted(const TomboURI *pURI)\r
121 {\r
122         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
123         if (pImpl == NULL) return FALSE;\r
124         return pImpl->IsEncrypted(pURI);\r
125 }\r
126 \r
127 BOOL Repository::Delete(const TomboURI *pURI, URIOption *pOption)\r
128 {\r
129         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
130         if (pImpl == NULL) return FALSE;\r
131         return pImpl->Delete(pURI, pOption);\r
132 }\r
133 \r
134 BOOL Repository::Copy(const TomboURI *pCopyFrom, const TomboURI *pCopyTo, URIOption *pOption)\r
135 {\r
136         // XXXX over subrepository is not supported yet.\r
137         RepositoryImpl *pImpl = GetAssocSubRepository(pCopyFrom);\r
138         if (pImpl == NULL) return FALSE;\r
139         return pImpl->Copy(pCopyFrom, pCopyTo, pOption);\r
140 }\r
141 \r
142 BOOL Repository::Move(const TomboURI *pMoveFrom, const TomboURI *pMoveTo, URIOption *pOption)\r
143 {\r
144         // XXXX over subrepository is not supported yet.\r
145         RepositoryImpl *pImpl = GetAssocSubRepository(pMoveFrom);\r
146         if (pImpl == NULL) return FALSE;\r
147         return pImpl->Move(pMoveFrom, pMoveTo, pOption);\r
148 }\r
149 \r
150 BOOL Repository::GetPhysicalPath(const TomboURI *pURI, TString *pFullPath)\r
151 {\r
152         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
153         if (pImpl == NULL) return FALSE;\r
154         return pImpl->GetPhysicalPath(pURI, pFullPath);\r
155 }\r
156 \r
157 BOOL Repository::ChangeHeadLine(const TomboURI *pURI, LPCTSTR pReqNewHeadLine, URIOption *pOption)\r
158 {\r
159         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
160         if (pImpl == NULL) return FALSE;\r
161         return pImpl->ChangeHeadLine(pURI, pReqNewHeadLine, pOption);\r
162 }\r
163 \r
164 \r
165 URIList *Repository::GetChild(const TomboURI *pFolder, BOOL bSkipEncrypt, BOOL bLooseDecrypt, BOOL *pLoose)\r
166 {\r
167         RepositoryImpl *pImpl = GetAssocSubRepository(pFolder);\r
168         if (pImpl == NULL) return FALSE;\r
169         return pImpl->GetChild(pFolder, bSkipEncrypt, bLooseDecrypt, pLoose);\r
170 }\r
171 \r
172 BOOL Repository::RequestAllocateURI(const TomboURI *pBaseURI, LPCTSTR pText, TString *pHeadLine, TomboURI *pURI, const TomboURI *pTemplateURI)\r
173 {\r
174         RepositoryImpl *pImpl = GetAssocSubRepository(pBaseURI);\r
175         if (pImpl == NULL) return FALSE;\r
176         return pImpl->RequestAllocateURI(pBaseURI, pText, pHeadLine, pURI, pTemplateURI);\r
177 }\r
178 \r
179 BOOL Repository::GetAttribute(const TomboURI *pURI, NoteAttribute *pAttribute)\r
180 {\r
181         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
182         if (pImpl == NULL) return FALSE;\r
183         return pImpl->GetAttribute(pURI, pAttribute);\r
184 }\r
185 \r
186 BOOL Repository::GetNoteAttribute(const TomboURI *pURI, UINT64 *pLastUpdate, UINT64 *pCreateDate, UINT64 *pFileSize)\r
187 {\r
188         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
189         if (pImpl == NULL) return FALSE;\r
190         return pImpl->GetNoteAttribute(pURI, pLastUpdate, pCreateDate, pFileSize);\r
191 }\r
192 \r
193 BOOL Repository::SetAttribute(const TomboURI *pURI, const NoteAttribute *pAttribute)\r
194 {\r
195         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
196         if (pImpl == NULL) return FALSE;\r
197         return pImpl->SetAttribute(pURI, pAttribute);\r
198 }\r
199 \r
200 LPTSTR Repository::GetNoteData(const TomboURI *pURI)\r
201 {\r
202         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
203         if (pImpl == NULL) return FALSE;\r
204         return pImpl->GetNoteData(pURI);\r
205 }\r
206 \r
207 LPBYTE Repository::GetNoteDataNative(const TomboURI *pURI, LPDWORD pSize)\r
208 {\r
209         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
210         if (pImpl == NULL) return FALSE;\r
211         return pImpl->GetNoteDataNative(pURI, pSize);\r
212 }\r
213 \r
214 BOOL Repository::ExecuteAssoc(const TomboURI *pURI, ExeAppType nType)\r
215 {\r
216         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
217         if (pImpl == NULL) return FALSE;\r
218         return pImpl->ExecuteAssoc(pURI, nType);\r
219 }\r
220 \r
221 BOOL Repository::MakeFolder(const TomboURI *pParent, LPCTSTR pFolderName)\r
222 {\r
223         RepositoryImpl *pImpl = GetAssocSubRepository(pParent);\r
224         if (pImpl == NULL) return FALSE;\r
225         return pImpl->MakeFolder(pParent, pFolderName);\r
226 }\r
227 \r
228 BOOL Repository::GetFileName(const TomboURI *pURI, TString *pName)\r
229 {\r
230         RepositoryImpl *pImpl = GetAssocSubRepository(pURI);\r
231         if (pImpl == NULL) return FALSE;\r
232         return pURI->GetBaseName(pName);\r
233 }\r
234 \r
235 /////////////////////////////////////////\r
236 // sub repository IF\r
237 /////////////////////////////////////////\r
238 \r
239 DWORD Repository::GetNumOfSubRepository()\r
240 {\r
241         return vSubRepository.NumItems();\r
242 }\r
243 \r
244 DWORD Repository::GetSubRepositoryType(DWORD nIndex)\r
245 {\r
246         RepositoryImpl *pImpl = *vSubRepository.GetUnit(nIndex);\r
247         if (pImpl == NULL) return NULL;\r
248 \r
249         return pImpl->GetRepositoryType();\r
250 }\r
251 \r
252 LPCTSTR Repository::GetSubRepositoryName(DWORD nIndex)\r
253 {\r
254         RepositoryImpl *pImpl = *vSubRepository.GetUnit(nIndex);\r
255         if (pImpl == NULL) return NULL;\r
256 \r
257         return pImpl->GetDisplayName();\r
258 }\r
259 \r
260 const TomboURI *Repository::GetSubRepositoryRootURI(DWORD nIndex)\r
261 {\r
262         RepositoryImpl *pImpl = *vSubRepository.GetUnit(nIndex);\r
263         if (pImpl == NULL) return NULL;\r
264         return pImpl->GetRootURI();\r
265 }\r
266 \r
267 LPTSTR Repository::GetSubRepoXMLSaveString(DWORD nIndex)\r
268 {\r
269         RepositoryImpl *pImpl = *vSubRepository.GetUnit(nIndex);\r
270         if (pImpl == NULL) return NULL;\r
271         return pImpl->GetXMLSaveString();\r
272 }\r
273 \r
274 // factory methods for RepositoryImpl\r
275 RepositoryImpl *Repository::CreateSubRepo(LPCWSTR pName, const WCHAR **atts)\r
276 {\r
277         if (wcscmp(pName, L"localfile") == 0) {\r
278                 DWORD i = 0;\r
279                 LPTSTR pDispName = NULL;\r
280                 LPTSTR pRepName = NULL;\r
281                 LPTSTR pTopDir = NULL;\r
282                 BOOL bKeepTitle = FALSE;\r
283                 BOOL bKeepCaret = FALSE;\r
284                 BOOL bSafeFileName = FALSE;\r
285 \r
286                 while (atts[i] != L'\0') {\r
287                         if (wcscmp(atts[i], L"name") == 0) {\r
288                                 pRepName = ConvWCharToTChar(atts[i + 1]);\r
289                         } else if (wcscmp(atts[i], L"dispname") == 0) {\r
290                                 pDispName = ConvWCharToTChar(atts[i + 1]);\r
291                         } else if (wcscmp(atts[i], L"path") == 0) {\r
292                                 pTopDir = ConvWCharToTChar(atts[i + 1]);\r
293                         } else if (wcscmp(atts[i], L"keeptitle") == 0) {\r
294                                 bKeepTitle = (wcscmp(atts[i + 1], L"1") == 0);\r
295                         } else if (wcscmp(atts[i], L"keepcaret") == 0) {\r
296                                 bKeepCaret = (wcscmp(atts[i + 1], L"1") == 0);\r
297                         } else if (wcscmp(atts[i], L"safefilename") == 0) {\r
298                                 bSafeFileName = (wcscmp(atts[i + 1], L"1") == 0);\r
299                         }\r
300                         i+= 2;\r
301                 }\r
302                 ArrayAutoPointer<TCHAR> ap1(pDispName), ap2(pRepName), ap3(pTopDir);\r
303 \r
304                 if (pRepName == NULL || pDispName == NULL || pTopDir == NULL) {\r
305                         SetLastError(ERROR_INVALID_PARAMETER);\r
306                         return NULL;\r
307                 }\r
308 \r
309                 LocalFileRepository *pImpl = new LocalFileRepository();\r
310                 if (pImpl == NULL) {\r
311                         SetLastError(ERROR_NOT_ENOUGH_MEMORY);\r
312                         return NULL;\r
313                 }\r
314                 if (!pImpl->Init(pRepName, pDispName, pTopDir, bKeepTitle, bKeepCaret, bSafeFileName)) {\r
315                         delete[] pImpl;\r
316                         return NULL;\r
317                 }\r
318                 return pImpl;\r
319         } else if (wcscmp(pName, L"vfolder") == 0) {\r
320                 LPTSTR pDispName = NULL;\r
321                 LPTSTR pRepName = NULL;\r
322                 DWORD i = 0;\r
323                 while (atts[i] != L'\0') {\r
324                         if (wcscmp(atts[i], L"name") == 0) {\r
325                                 pRepName = ConvWCharToTChar(atts[i + 1]);\r
326                         } else if (wcscmp(atts[i], L"dispname") == 0) {\r
327                                 pDispName = ConvWCharToTChar(atts[i + 1]);\r
328                         }\r
329                         i += 2;\r
330                 }\r
331                 ArrayAutoPointer<TCHAR> ap1(pDispName), ap2(pRepName);\r
332 \r
333                 VFolderRepository *pImpl = new VFolderRepository();\r
334                 if (pImpl == NULL) {\r
335                         SetLastError(ERROR_NOT_ENOUGH_MEMORY);\r
336                         return NULL;\r
337                 }\r
338                 if (!pImpl->Init(pRepName, pDispName)) {\r
339                         delete[] pImpl;\r
340                         return NULL;\r
341                 }\r
342                 return pImpl;\r
343         } else {\r
344                 return NULL;\r
345         }\r
346 }\r