| 1 |
#include <windows.h> |
| 2 |
#include <tchar.h> |
| 3 |
|
| 4 |
#include "Tombo.h" |
| 5 |
#include "Message.h" |
| 6 |
#include "UniConv.h" |
| 7 |
#include "TString.h" |
| 8 |
#include "TomboURI.h" |
| 9 |
#include "Property.h" |
| 10 |
#include "Repository.h" |
| 11 |
#include "RepositoryImpl.h" |
| 12 |
|
| 13 |
#include "MemoNote.h" |
| 14 |
#include "AutoPtr.h" |
| 15 |
#include "PasswordManager.h" |
| 16 |
#include "MemoInfo.h" |
| 17 |
|
| 18 |
#include "DirList.h" |
| 19 |
#include "URIScanner.h" |
| 20 |
#include "DirectoryScanner.h" |
| 21 |
#include "MemoFolder.h" |
| 22 |
|
| 23 |
#if defined(PLATFORM_BE500) |
| 24 |
#include <CoShellapi.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
|
| 28 |
///////////////////////////////////////// |
| 29 |
// static funcs |
| 30 |
///////////////////////////////////////// |
| 31 |
|
| 32 |
#define DEFAULT_HEADLINE MSG_DEFAULT_HEADLINE |
| 33 |
|
| 34 |
static int ChopFileNumberLen(LPTSTR pHeadLine); |
| 35 |
static BOOL IsFileExist(LPCTSTR pFileName); |
| 36 |
|
| 37 |
///////////////////////////////////////// |
| 38 |
// Repository ctor & dtor, initializer |
| 39 |
///////////////////////////////////////// |
| 40 |
|
| 41 |
RepositoryImpl::RepositoryImpl() : pRepName(NULL), pDispName(NULL), pRootURI(NULL) {} |
| 42 |
RepositoryImpl::~RepositoryImpl() |
| 43 |
{ |
| 44 |
delete [] pRepName; |
| 45 |
delete [] pDispName; |
| 46 |
delete pRootURI; |
| 47 |
} |
| 48 |
|
| 49 |
BOOL RepositoryImpl::Init(LPCTSTR rep, LPCTSTR disp, DWORD ntype) |
| 50 |
{ |
| 51 |
pRepName = StringDup(rep); |
| 52 |
pDispName = StringDup(disp); |
| 53 |
nRepNameLen = _tcslen(pRepName); |
| 54 |
|
| 55 |
nRepType = ntype; |
| 56 |
|
| 57 |
TString sStrURI; |
| 58 |
if (!sStrURI.Join(TEXT("tombo://"), rep, TEXT("/"))) return FALSE; |
| 59 |
pRootURI = new TomboURI(); |
| 60 |
if (!pRootURI->Init(sStrURI.Get())) return FALSE; |
| 61 |
|
| 62 |
return (pRepName != NULL) && (pDispName != NULL); |
| 63 |
} |
| 64 |
|
| 65 |
///////////////////////////////////////// |
| 66 |
// Is the note encrypted? |
| 67 |
///////////////////////////////////////// |
| 68 |
|
| 69 |
BOOL RepositoryImpl::IsEncrypted(const TomboURI *pURI) |
| 70 |
{ |
| 71 |
URIOption opt(NOTE_OPTIONMASK_ENCRYPTED); |
| 72 |
GetOption(pURI, &opt); |
| 73 |
return opt.bEncrypt; |
| 74 |
} |
| 75 |
|
| 76 |
///////////////////////////////////////// |
| 77 |
// ctor & dtor, initializer |
| 78 |
///////////////////////////////////////// |
| 79 |
|
| 80 |
LocalFileRepository::LocalFileRepository() : pTopDir(NULL) |
| 81 |
{ |
| 82 |
} |
| 83 |
|
| 84 |
LocalFileRepository::~LocalFileRepository() |
| 85 |
{ |
| 86 |
delete[] pTopDir; |
| 87 |
} |
| 88 |
|
| 89 |
BOOL LocalFileRepository::Init(LPCTSTR rep, LPCTSTR disp, LPCTSTR dir, |
| 90 |
BOOL title, BOOL caret, BOOL safefile) |
| 91 |
{ |
| 92 |
if (!RepositoryImpl::Init(rep, disp, TOMBO_REPO_SUBREPO_TYPE_LOCALFILE)) return FALSE; |
| 93 |
|
| 94 |
pTopDir = StringDup(dir); |
| 95 |
bKeepTitle = title; |
| 96 |
bKeepCaret = caret; |
| 97 |
bSafeFileName = safefile; |
| 98 |
return TRUE; |
| 99 |
} |
| 100 |
|
| 101 |
RepositoryImpl *LocalFileRepository::Clone() |
| 102 |
{ |
| 103 |
LocalFileRepository *pImpl = new LocalFileRepository(); |
| 104 |
if (pImpl == NULL) { |
| 105 |
SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
| 106 |
return NULL; |
| 107 |
} |
| 108 |
if (!pImpl->Init(GetRepositoryName(), GetDisplayName(), pTopDir, bKeepTitle, bKeepCaret, bSafeFileName)) { |
| 109 |
delete[] pImpl; |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
return pImpl; |
| 113 |
} |
| 114 |
|
| 115 |
LPTSTR LocalFileRepository::GetXMLSaveString() |
| 116 |
{ |
| 117 |
TString sXMLStr; |
| 118 |
if (!sXMLStr.Join(TEXT(" <localfile name=\""), GetRepositoryName(), TEXT("\" dispname=\""), GetDisplayName())) return NULL; |
| 119 |
if (!sXMLStr.StrCat(TEXT("\" path=\""))) return NULL; |
| 120 |
if (!sXMLStr.StrCat(pTopDir)) return NULL; |
| 121 |
if (!sXMLStr.StrCat(TEXT("\" keeptitle=\""))) return NULL; |
| 122 |
if (!sXMLStr.StrCat(bKeepTitle ? TEXT("1") : TEXT("0"))) return NULL; |
| 123 |
if (!sXMLStr.StrCat(TEXT("\" keepcaret=\""))) return NULL; |
| 124 |
if (!sXMLStr.StrCat(bKeepCaret ? TEXT("1") : TEXT("0"))) return NULL; |
| 125 |
if (!sXMLStr.StrCat(TEXT("\" safefilename=\""))) return NULL; |
| 126 |
if (!sXMLStr.StrCat(bSafeFileName ? TEXT("1") : TEXT("0"))) return NULL; |
| 127 |
if (!sXMLStr.StrCat(TEXT("\"/>\n"))) return NULL; |
| 128 |
return StringDup(sXMLStr.Get()); |
| 129 |
} |
| 130 |
|
| 131 |
BOOL LocalFileRepository::SetTopDir(LPCTSTR pDir) |
| 132 |
{ |
| 133 |
delete [] pTopDir; |
| 134 |
pTopDir = StringDup(pDir); |
| 135 |
return pTopDir != NULL; |
| 136 |
} |
| 137 |
|
| 138 |
///////////////////////////////////////// |
| 139 |
// get attribute file path from notes URI |
| 140 |
///////////////////////////////////////// |
| 141 |
|
| 142 |
BOOL LocalFileRepository::GetTDTFullPath(const TomboURI *pURI, TString *pTdtName) |
| 143 |
{ |
| 144 |
TString sPath; |
| 145 |
if (!pURI->GetFilePath(&sPath)) return FALSE; |
| 146 |
return pTdtName->Join(pTopDir, TEXT("\\"), sPath.Get(), TEXT(".tdt")); |
| 147 |
} |
| 148 |
|
| 149 |
///////////////////////////////////////// |
| 150 |
// Update note |
| 151 |
///////////////////////////////////////// |
| 152 |
|
| 153 |
BOOL LocalFileRepository::Update(const TomboURI *pCurrentURI, LPCTSTR pData, |
| 154 |
TomboURI *pNewURI, TString *pNewHeadLine) |
| 155 |
{ |
| 156 |
// Save note data |
| 157 |
if (!Save(pCurrentURI, pData, pNewURI, pNewHeadLine)) { |
| 158 |
return FALSE; |
| 159 |
} |
| 160 |
|
| 161 |
// if tdt exists, move one |
| 162 |
TString sCurrentTdtPath, sNewTdtPath; |
| 163 |
if (!GetTDTFullPath(pCurrentURI, &sCurrentTdtPath)) return FALSE; |
| 164 |
if (!GetTDTFullPath(pNewURI, &sNewTdtPath)) return FALSE; |
| 165 |
|
| 166 |
MemoInfo mi(pTopDir); |
| 167 |
mi.RenameInfo(sCurrentTdtPath.Get(), sNewTdtPath.Get()); |
| 168 |
return TRUE; |
| 169 |
} |
| 170 |
|
| 171 |
///////////////////////////////////////// |
| 172 |
// Update's subfunction |
| 173 |
///////////////////////////////////////// |
| 174 |
BOOL LocalFileRepository::Save(const TomboURI *pCurrentURI, LPCTSTR pMemo, |
| 175 |
TomboURI *pNewURI, TString *pNewHeadLine) |
| 176 |
{ |
| 177 |
MemoNote *pNote = MemoNote::MemoNoteFactory(pCurrentURI); |
| 178 |
if (pNote == NULL) return FALSE; |
| 179 |
AutoPointer<MemoNote> apNote(pNote); |
| 180 |
|
| 181 |
TString sOrigFile; |
| 182 |
if (!GetPhysicalPath(pCurrentURI, &sOrigFile)) return FALSE; |
| 183 |
|
| 184 |
// get current headline from path |
| 185 |
if (!GetHeadLine(pCurrentURI, pNewHeadLine)) return FALSE; |
| 186 |
|
| 187 |
URIOption opt(NOTE_OPTIONMASK_SAFEFILE); |
| 188 |
if (!GetOption(pCurrentURI, &opt)) return FALSE; |
| 189 |
|
| 190 |
if (bKeepTitle || opt.bSafeFileName) { |
| 191 |
if (!SaveIfHeadLineIsNotChanged(pNote, pMemo, sOrigFile.Get())) return FALSE; |
| 192 |
|
| 193 |
// URI is not changed. |
| 194 |
return pNewURI->Init(*pCurrentURI); |
| 195 |
|
| 196 |
} else { |
| 197 |
// Get new headline from memo text |
| 198 |
TString sHeadLine; |
| 199 |
if (!MemoNote::GetHeadLineFromMemoText(pMemo, &sHeadLine)) return FALSE; |
| 200 |
|
| 201 |
DWORD nH = ChopFileNumberLen(pNewHeadLine->Get()); |
| 202 |
DWORD nH2 = ChopFileNumberLen(sHeadLine.Get()); |
| 203 |
|
| 204 |
BOOL bResult; |
| 205 |
// check headline has changed. |
| 206 |
if (nH == nH2 && _tcsncmp(pNewHeadLine->Get(), sHeadLine.Get(), nH) == 0) { |
| 207 |
bResult = SaveIfHeadLineIsNotChanged(pNote, pMemo, sOrigFile.Get()); |
| 208 |
} else { |
| 209 |
bResult = SaveIfHeadLineIsChanged(pNote, pMemo, sOrigFile.Get(), |
| 210 |
sHeadLine.Get(), pNewHeadLine); |
| 211 |
} |
| 212 |
if (bResult) { |
| 213 |
bResult = pNote->GetURI(GetRepositoryName(), pNewURI); |
| 214 |
} |
| 215 |
return bResult; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
BOOL LocalFileRepository::SaveIfHeadLineIsChanged( |
| 220 |
MemoNote *pNote, LPCTSTR pMemo, LPCTSTR pOrigFile, LPCTSTR pHeadLine, |
| 221 |
TString *pNewHeadLine) |
| 222 |
{ |
| 223 |
LPCTSTR pNotePath = pNote->MemoPath(); |
| 224 |
|
| 225 |
// changed. |
| 226 |
TString sMemoDir; |
| 227 |
TString sNewFile; |
| 228 |
|
| 229 |
if (!sMemoDir.GetDirectoryPath(pNotePath)) return FALSE; |
| 230 |
if (!MemoNote::GetHeadLinePath(pTopDir, sMemoDir.Get(), pHeadLine, pNote->GetExtension(), |
| 231 |
&sNewFile, &pNotePath, pNewHeadLine)) return FALSE; |
| 232 |
|
| 233 |
// write data |
| 234 |
BOOL bResult = pNote->SaveDataT(g_pPassManager, pMemo, sNewFile.Get()); |
| 235 |
if (bResult) { |
| 236 |
// delete original file |
| 237 |
DeleteFile(pOrigFile); |
| 238 |
|
| 239 |
// Additionally, rename memo info(*.tdt) file. |
| 240 |
if (bKeepCaret) { |
| 241 |
MemoInfo mi(pTopDir); |
| 242 |
mi.RenameInfo(pOrigFile, sNewFile.Get()); |
| 243 |
} |
| 244 |
|
| 245 |
// Update note's file path information. |
| 246 |
if (!pNote->SetMemoPath(pNotePath)) return FALSE; |
| 247 |
|
| 248 |
return TRUE; |
| 249 |
|
| 250 |
} else { |
| 251 |
// rollback (delete new writing file) |
| 252 |
DeleteFile(sNewFile.Get()); |
| 253 |
return FALSE; |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
BOOL LocalFileRepository::SaveIfHeadLineIsNotChanged(MemoNote *pNote, LPCTSTR pMemo, LPCTSTR pOrigFile) |
| 258 |
{ |
| 259 |
// Generate backup file name |
| 260 |
TString sBackupFile; |
| 261 |
if (!sBackupFile.Join(pOrigFile, TEXT(".tmp"))) return FALSE; |
| 262 |
// Backup(copy) original file |
| 263 |
// |
| 264 |
// Because ActiveSync can't treat Move&Write, backup operation uses not move but copy |
| 265 |
if (!CopyFile(pOrigFile, sBackupFile.Get(), FALSE)) { |
| 266 |
// if new file, copy are failed but it is OK. |
| 267 |
if (GetLastError() != ERROR_FILE_NOT_FOUND) return FALSE; |
| 268 |
} |
| 269 |
|
| 270 |
// Save to file |
| 271 |
if (!pNote->SaveDataT(g_pPassManager, pMemo, pOrigFile)) { |
| 272 |
// When save failed, try to rollback original file. |
| 273 |
DeleteFile(pOrigFile); |
| 274 |
MoveFile(sBackupFile.Get(), pOrigFile); |
| 275 |
return FALSE; |
| 276 |
} |
| 277 |
// remove backup file |
| 278 |
DeleteFile(sBackupFile.Get()); |
| 279 |
return TRUE; |
| 280 |
} |
| 281 |
|
| 282 |
//////////////////////////////////////////////////////// |
| 283 |
// remove "(n)" from headline |
| 284 |
//////////////////////////////////////////////////////// |
| 285 |
|
| 286 |
static int ChopFileNumberLen(LPTSTR pHeadLine) |
| 287 |
{ |
| 288 |
if (*pHeadLine == TEXT('\0')) return 0; |
| 289 |
|
| 290 |
DWORD n = _tcslen(pHeadLine); |
| 291 |
LPTSTR p = pHeadLine + n - 1; |
| 292 |
if (*p != TEXT(')')) return n; |
| 293 |
p--; |
| 294 |
while(p >= pHeadLine) { |
| 295 |
if (*p == TEXT('(')) { |
| 296 |
return p - pHeadLine; |
| 297 |
} |
| 298 |
if (*p < TEXT('0') || *p > TEXT('9')) break; |
| 299 |
p--; |
| 300 |
} |
| 301 |
return n; |
| 302 |
} |
| 303 |
|
| 304 |
///////////////////////////////////////////// |
| 305 |
// file existance check |
| 306 |
///////////////////////////////////////////// |
| 307 |
|
| 308 |
static BOOL IsFileExist(LPCTSTR pFileName) |
| 309 |
{ |
| 310 |
HANDLE hFile = CreateFile(pFileName, GENERIC_READ, FILE_SHARE_WRITE, NULL, |
| 311 |
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 312 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 313 |
return FALSE; |
| 314 |
} else { |
| 315 |
CloseHandle(hFile); |
| 316 |
return TRUE; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
///////////////////////////////////////// |
| 321 |
// Get Headline string |
| 322 |
///////////////////////////////////////// |
| 323 |
// This method ask password if it need. |
| 324 |
|
| 325 |
BOOL LocalFileRepository::GetHeadLine(const TomboURI *pURI, TString *pHeadLine) |
| 326 |
{ |
| 327 |
BOOL bIsLeaf = pURI->IsLeaf(); |
| 328 |
|
| 329 |
// check if the uri is safename |
| 330 |
if (bIsLeaf) { |
| 331 |
LPCTSTR pURIstr = pURI->GetFullURI(); |
| 332 |
DWORD n = _tcslen(pURIstr); |
| 333 |
if (n > 4 && _tcscmp(pURIstr + n - 4, TEXT(".chs")) == 0) { |
| 334 |
TString sPath; |
| 335 |
if (!pURI->GetFilePath(&sPath)) return FALSE; |
| 336 |
CryptedMemoNote cn; |
| 337 |
cn.Init(sPath.Get()); |
| 338 |
LPTSTR p = cn.GetMemoBody(pTopDir, g_pPassManager); |
| 339 |
if (p == NULL) return FALSE; |
| 340 |
SecureBufferAutoPointerT ap(p); |
| 341 |
if (!MemoNote::GetHeadLineFromMemoText(p, pHeadLine)) return FALSE; |
| 342 |
return TRUE; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
if (!pURI->GetBaseName(pHeadLine)) return FALSE; |
| 347 |
|
| 348 |
if (_tcslen(pHeadLine->Get()) == 0) { |
| 349 |
// root |
| 350 |
return pHeadLine->Set(TEXT("[root]")); |
| 351 |
} |
| 352 |
|
| 353 |
if (bIsLeaf) { |
| 354 |
LPTSTR p = pHeadLine->Get(); |
| 355 |
DWORD n = _tcslen(p); |
| 356 |
if (n > 4) { |
| 357 |
*(p + n - 4) = TEXT('\0'); |
| 358 |
} |
| 359 |
} |
| 360 |
return TRUE; |
| 361 |
} |
| 362 |
|
| 363 |
///////////////////////////////////////// |
| 364 |
// Get option information |
| 365 |
///////////////////////////////////////// |
| 366 |
|
| 367 |
BOOL LocalFileRepository::GetOption(const TomboURI *pURI, URIOption *pOption) const |
| 368 |
{ |
| 369 |
if ((pOption->nFlg & NOTE_OPTIONMASK_VALID) || (pOption->nFlg & NOTE_OPTIONMASK_ICON)) { |
| 370 |
LPCTSTR p = pURI->GetFullURI(); |
| 371 |
DWORD len = _tcslen(p); |
| 372 |
if (_tcscmp(p + len - 1, TEXT("/")) == 0) { |
| 373 |
// folder |
| 374 |
pOption->bValid = TRUE; |
| 375 |
pOption->bFolder = TRUE; |
| 376 |
} else { |
| 377 |
// file |
| 378 |
p = p + len - 4; |
| 379 |
if (_tcsicmp(p, TEXT(".txt")) == 0 || |
| 380 |
_tcsicmp(p, TEXT(".chi")) == 0 || |
| 381 |
_tcsicmp(p, TEXT(".chs")) == 0) { |
| 382 |
pOption->bValid = TRUE; |
| 383 |
pOption->bFolder = FALSE; |
| 384 |
} else { |
| 385 |
pOption->bValid = FALSE; |
| 386 |
return TRUE; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
if ((pOption->nFlg & NOTE_OPTIONMASK_ENCRYPTED) || (pOption->nFlg & NOTE_OPTIONMASK_ICON)) { |
| 391 |
LPCTSTR p = pURI->GetFullURI(); |
| 392 |
DWORD n = _tcslen(p); |
| 393 |
if (n > 4) { |
| 394 |
if (_tcsicmp(p + n - 4, TEXT(".chi")) == 0 || |
| 395 |
_tcsicmp(p + n - 4, TEXT(".chs")) == 0) { |
| 396 |
pOption->bEncrypt = TRUE; |
| 397 |
} else { |
| 398 |
pOption->bEncrypt = FALSE; |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
if (pOption->nFlg & NOTE_OPTIONMASK_SAFEFILE) { |
| 404 |
LPCTSTR p = pURI->GetFullURI(); |
| 405 |
if (_tcslen(p) > 4 && _tcscmp(p + _tcslen(p) - 4, TEXT(".chs")) == 0) { |
| 406 |
pOption->bSafeFileName = TRUE; |
| 407 |
} else { |
| 408 |
pOption->bSafeFileName = FALSE; |
| 409 |
} |
| 410 |
} |
| 411 |
if (pOption->nFlg & NOTE_OPTIONMASK_ICON) { |
| 412 |
if (pOption->bFolder) { |
| 413 |
pOption->iIcon = IMG_FOLDER; |
| 414 |
} else { |
| 415 |
if (pOption->bEncrypt) { |
| 416 |
pOption->iIcon = IMG_ARTICLE_ENCRYPTED; |
| 417 |
} else { |
| 418 |
pOption->iIcon = IMG_ARTICLE; |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
return TRUE; |
| 423 |
} |
| 424 |
|
| 425 |
///////////////////////////////////////// |
| 426 |
// Set option information |
| 427 |
///////////////////////////////////////// |
| 428 |
|
| 429 |
BOOL LocalFileRepository::SetOption(const TomboURI *pCurrentURI, URIOption *pOption) |
| 430 |
{ |
| 431 |
if (pOption->nFlg & NOTE_OPTIONMASK_ENCRYPTED) { |
| 432 |
if (pCurrentURI->IsLeaf()) { |
| 433 |
// set option to file |
| 434 |
if (pOption->bEncrypt) { |
| 435 |
// encrypt |
| 436 |
return EncryptLeaf(pCurrentURI, pOption); |
| 437 |
} else { |
| 438 |
// decrypt |
| 439 |
return DecryptLeaf(pCurrentURI, pOption); |
| 440 |
} |
| 441 |
} else { |
| 442 |
// set option to folder |
| 443 |
return EnDecryptFolder(pCurrentURI, pOption); |
| 444 |
} |
| 445 |
} |
| 446 |
return TRUE; |
| 447 |
} |
| 448 |
|
| 449 |
///////////////////////////////////////// |
| 450 |
// Get safe file name |
| 451 |
///////////////////////////////////////// |
| 452 |
|
| 453 |
BOOL LocalFileRepository::GetSafeFileName(const TString *pBasePath, TString *pNewName) |
| 454 |
{ |
| 455 |
TString s; |
| 456 |
|
| 457 |
DWORD l = _tcslen(pBasePath->Get()) + 20 + 1; |
| 458 |
if (!s.Alloc(l)) return FALSE; |
| 459 |
_tcscpy(s.Get(), pBasePath->Get()); |
| 460 |
_tcscat(s.Get(), TEXT("0000000000000000.chs")); |
| 461 |
|
| 462 |
LPTSTR pFileNamePart = s.Get() + _tcslen(pBasePath->Get()); |
| 463 |
int nw; |
| 464 |
do { |
| 465 |
// generate 10digit random number |
| 466 |
nw = rand() % 10000; |
| 467 |
wsprintf(pFileNamePart, TEXT("%04d"), nw); |
| 468 |
nw = rand() % 10000; |
| 469 |
wsprintf(pFileNamePart + 4, TEXT("%04d"), nw); |
| 470 |
nw = rand() % 10000; |
| 471 |
wsprintf(pFileNamePart + 8, TEXT("%04d"), nw); |
| 472 |
nw = rand() % 10000; |
| 473 |
wsprintf(pFileNamePart + 12, TEXT("%04d"), nw); |
| 474 |
_tcscpy(pFileNamePart + 16, TEXT(".chs")); |
| 475 |
} while(IsFileExist(s.Get())); // if same name exists, retry it |
| 476 |
|
| 477 |
if (!pNewName->Set(pFileNamePart)) return FALSE; |
| 478 |
|
| 479 |
return TRUE; |
| 480 |
} |
| 481 |
|
| 482 |
///////////////////////////////////////// |
| 483 |
// Decide new allocated filename |
| 484 |
///////////////////////////////////////// |
| 485 |
|
| 486 |
BOOL LocalFileRepository::NegotiateNewName(LPCTSTR pMemoPath, LPCTSTR pText, LPCTSTR pMemoDir, |
| 487 |
TString *pFullPath, LPCTSTR *ppNotePath, TString *pNewHeadLine) |
| 488 |
{ |
| 489 |
TString sHeadLine; |
| 490 |
|
| 491 |
if (bSafeFileName) { |
| 492 |
TString sBase; |
| 493 |
TString sNewName; |
| 494 |
if (!sBase.Join(pTopDir, TEXT("\\"), pMemoDir)) return FALSE; |
| 495 |
if (!GetSafeFileName(&sBase, &sNewName)) return FALSE; |
| 496 |
|
| 497 |
if (!pFullPath->Join(sBase.Get(), sNewName.Get())) return FALSE; |
| 498 |
|
| 499 |
*ppNotePath = pFullPath->Get() + _tcslen(pTopDir) + 1; |
| 500 |
if (!MemoNote::GetHeadLineFromMemoText(pText, pNewHeadLine)) return FALSE; |
| 501 |
} else { |
| 502 |
if (bKeepTitle) { |
| 503 |
if (!MemoNote::GetHeadLineFromFilePath(pMemoPath, &sHeadLine)) return FALSE; |
| 504 |
} else { |
| 505 |
if (!MemoNote::GetHeadLineFromMemoText(pText, &sHeadLine)) return FALSE; |
| 506 |
} |
| 507 |
|
| 508 |
if (!MemoNote::GetHeadLinePath(pTopDir, pMemoDir, sHeadLine.Get(), TEXT(".chi"), |
| 509 |
pFullPath, ppNotePath, pNewHeadLine)) { |
| 510 |
return FALSE; |
| 511 |
} |
| 512 |
} |
| 513 |
return TRUE; |
| 514 |
} |
| 515 |
|
| 516 |
////////////////////////////// |
| 517 |
// Encrypt file |
| 518 |
// |
| 519 |
// assume pCurURI is Leaf. |
| 520 |
|
| 521 |
TomboURI *LocalFileRepository::DoEncryptFile(const TomboURI *pOldURI, MemoNote *pNote, TString *pHeadLine) |
| 522 |
{ |
| 523 |
TString sMemoDir; |
| 524 |
if (!sMemoDir.GetDirectoryPath(pNote->MemoPath())) return NULL; |
| 525 |
|
| 526 |
// Get plain memo data from file |
| 527 |
LPTSTR pText = pNote->GetMemoBody(pTopDir, g_pPassManager); |
| 528 |
if (pText == NULL) return NULL; |
| 529 |
SecureBufferAutoPointerT ap1(pText); |
| 530 |
|
| 531 |
TString sFullPath; |
| 532 |
LPCTSTR pNotePath; |
| 533 |
|
| 534 |
// Decide new name |
| 535 |
if (!NegotiateNewName(pNote->MemoPath(), pText, sMemoDir.Get(), |
| 536 |
&sFullPath, &pNotePath, pHeadLine)) return NULL; |
| 537 |
|
| 538 |
// Create new CyrptedMemoNote instance |
| 539 |
CryptedMemoNote *p = new CryptedMemoNote(); |
| 540 |
|
| 541 |
if (!p->Init(pNotePath)) return NULL; |
| 542 |
AutoPointer<CryptedMemoNote> ap2(p); |
| 543 |
|
| 544 |
// Save memo |
| 545 |
if (!p->SaveDataT(g_pPassManager, pText, sFullPath.Get())) return NULL; |
| 546 |
|
| 547 |
// rename TDT |
| 548 |
TString sOrigTDT; |
| 549 |
TString sNewTDT; |
| 550 |
if (GetTDTFullPath(pOldURI, &sOrigTDT) && |
| 551 |
sNewTDT.Join(sFullPath.Get(), TEXT(".tdt"))) { |
| 552 |
DeleteFile(sNewTDT.Get()); |
| 553 |
MoveFile(sOrigTDT.Get(), sNewTDT.Get()); |
| 554 |
} |
| 555 |
|
| 556 |
// generate new URI |
| 557 |
TomboURI *pURI = new TomboURI(); |
| 558 |
if (pURI == NULL || !p->GetURI(GetRepositoryName(), pURI)) return NULL; |
| 559 |
|
| 560 |
return pURI; |
| 561 |
} |
| 562 |
|
| 563 |
BOOL LocalFileRepository::EncryptLeaf(const TomboURI *pPlainURI, URIOption *pOption) |
| 564 |
{ |
| 565 |
MemoNote *pPlain = MemoNote::MemoNoteFactory(pPlainURI); |
| 566 |
AutoPointer<MemoNote> ap(pPlain); |
| 567 |
|
| 568 |
pOption->pNewHeadLine = new TString(); |
| 569 |
if (pOption->pNewHeadLine == NULL) return FALSE; |
| 570 |
|
| 571 |
pOption->pNewURI = DoEncryptFile(pPlainURI, pPlain, pOption->pNewHeadLine); |
| 572 |
if (pOption->pNewURI == NULL) return FALSE; |
| 573 |
|
| 574 |
if (!pPlain->DeleteMemoData(pTopDir)) { |
| 575 |
SetLastError(ERROR_TOMBO_W_DELETEOLD_FAILED); |
| 576 |
return FALSE; |
| 577 |
} |
| 578 |
|
| 579 |
return TRUE; |
| 580 |
} |
| 581 |
|
| 582 |
BOOL LocalFileRepository::DecryptLeaf(const TomboURI *pCurrentURI, URIOption *pOption) |
| 583 |
{ |
| 584 |
BOOL b; |
| 585 |
MemoNote *pCur = MemoNote::MemoNoteFactory(pCurrentURI); |
| 586 |
AutoPointer<MemoNote> ap(pCur); |
| 587 |
|
| 588 |
if ((pOption->pNewHeadLine = new TString()) == NULL) return FALSE; |
| 589 |
|
| 590 |
MemoNote *p = pCur->Decrypt(pTopDir, g_pPassManager, pOption->pNewHeadLine, &b); |
| 591 |
if (p == NULL) return FALSE; |
| 592 |
AutoPointer<MemoNote> ap2(p); |
| 593 |
|
| 594 |
pOption->pNewURI = new TomboURI(); |
| 595 |
if (pOption->pNewURI == NULL) return FALSE; |
| 596 |
if (!p->GetURI(GetRepositoryName(), pOption->pNewURI)) return FALSE; |
| 597 |
|
| 598 |
// rename TDT |
| 599 |
TString sOrigTDT; |
| 600 |
TString sNewTDT; |
| 601 |
if (GetTDTFullPath(pCurrentURI, &sOrigTDT) && |
| 602 |
GetTDTFullPath(pOption->pNewURI, &sNewTDT)) { |
| 603 |
DeleteFile(sNewTDT.Get()); |
| 604 |
MoveFile(sOrigTDT.Get(), sNewTDT.Get()); |
| 605 |
} |
| 606 |
|
| 607 |
if (!pCur->DeleteMemoData(pTopDir)) { |
| 608 |
SetLastError(ERROR_TOMBO_W_DELETEOLD_FAILED); |
| 609 |
return FALSE; |
| 610 |
} |
| 611 |
|
| 612 |
return TRUE; |
| 613 |
} |
| 614 |
|
| 615 |
///////////////////////////////////////// |
| 616 |
// Encrypt/Decrypt folder |
| 617 |
///////////////////////////////////////// |
| 618 |
|
| 619 |
BOOL LocalFileRepository::EnDecryptFolder(const TomboURI *pCurrentURI, URIOption *pOption) |
| 620 |
{ |
| 621 |
TString sPath; |
| 622 |
if (!GetPhysicalPath(pCurrentURI, &sPath)) return FALSE; |
| 623 |
|
| 624 |
DSEncrypt fc; |
| 625 |
if (!fc.Init(pTopDir, sPath.Get(), pCurrentURI->GetFullURI(), pOption->bEncrypt)) return FALSE; |
| 626 |
|
| 627 |
// ask password |
| 628 |
BOOL bCancel; |
| 629 |
const char *pPass = g_pPassManager->Password(&bCancel, pOption->bEncrypt); |
| 630 |
if (pPass == NULL) { |
| 631 |
SetLastError(ERROR_TOMBO_I_GET_PASSWORD_CANCELED); |
| 632 |
return FALSE; |
| 633 |
} |
| 634 |
|
| 635 |
// scan and encrypt/decrypt |
| 636 |
if (!fc.Scan() || fc.nNotEncrypted != 0) { |
| 637 |
SetLastError(ERROR_TOMBO_E_SOME_ERROR_OCCURED); |
| 638 |
return FALSE; |
| 639 |
} |
| 640 |
return TRUE; |
| 641 |
} |
| 642 |
|
| 643 |
///////////////////////////////////////// |
| 644 |
// Delete Note/folder |
| 645 |
///////////////////////////////////////// |
| 646 |
|
| 647 |
BOOL LocalFileRepository::Delete(const TomboURI *pURI, URIOption *pOption) |
| 648 |
{ |
| 649 |
if (pURI->IsLeaf()) { |
| 650 |
MemoNote *pNote = MemoNote::MemoNoteFactory(pURI); |
| 651 |
if (pNote == NULL) return FALSE; |
| 652 |
AutoPointer<MemoNote> ap(pNote); |
| 653 |
|
| 654 |
return pNote->DeleteMemoData(pTopDir); |
| 655 |
} else { |
| 656 |
TString sFullPath; |
| 657 |
if (!GetPhysicalPath(pURI, &sFullPath)) return FALSE; |
| 658 |
|
| 659 |
DSFileDelete fd; |
| 660 |
fd.Init(sFullPath.Get()); |
| 661 |
if (!fd.Scan()) { |
| 662 |
return FALSE; |
| 663 |
} |
| 664 |
} |
| 665 |
return TRUE; |
| 666 |
} |
| 667 |
|
| 668 |
///////////////////////////////////////// |
| 669 |
// Get Physical file/folder path |
| 670 |
///////////////////////////////////////// |
| 671 |
BOOL LocalFileRepository::GetPhysicalPath(const TomboURI *pURI, TString *pFullPath) |
| 672 |
{ |
| 673 |
if (!pFullPath->Alloc(_tcslen(pTopDir) + _tcslen(pURI->GetPath()) + 1)) return FALSE; |
| 674 |
LPCTSTR p = pURI->GetPath(); |
| 675 |
_tcscpy(pFullPath->Get(), pTopDir); |
| 676 |
LPTSTR q = pFullPath->Get() + _tcslen(pFullPath->Get()); |
| 677 |
|
| 678 |
while (*p) { |
| 679 |
#if defined(PLATFORM_WIN32) |
| 680 |
if (IsDBCSLeadByte(*p)) { |
| 681 |
*q++ = *p++; |
| 682 |
} |
| 683 |
#endif |
| 684 |
if (*p == TEXT('/')) { |
| 685 |
*q++ = TEXT('\\'); |
| 686 |
p++; |
| 687 |
continue; |
| 688 |
} |
| 689 |
*q++ = *p++; |
| 690 |
} |
| 691 |
*q = TEXT('\0'); |
| 692 |
return TRUE; |
| 693 |
} |
| 694 |
|
| 695 |
///////////////////////////////////////// |
| 696 |
|
| 697 |
static BOOL IsSubFolder(LPCTSTR pSrc, LPCTSTR pDst) |
| 698 |
{ |
| 699 |
DWORD n = _tcslen(pSrc); |
| 700 |
if (_tcsncmp(pSrc, pDst, n) == 0) return TRUE; |
| 701 |
return FALSE; |
| 702 |
} |
| 703 |
|
| 704 |
///////////////////////////////////////// |
| 705 |
// Copy note/folder |
| 706 |
///////////////////////////////////////// |
| 707 |
|
| 708 |
BOOL LocalFileRepository::Copy(const TomboURI *pCopyFrom, const TomboURI *pCopyTo, URIOption *pOption) |
| 709 |
{ |
| 710 |
URIOption opt1(NOTE_OPTIONMASK_ENCRYPTED | NOTE_OPTIONMASK_SAFEFILE | NOTE_OPTIONMASK_VALID); |
| 711 |
if (!GetOption(pCopyFrom, &opt1)) return FALSE; |
| 712 |
|
| 713 |
URIOption opt2(NOTE_OPTIONMASK_VALID); |
| 714 |
if (!GetOption(pCopyTo, &opt2)) return FALSE; |
| 715 |
|
| 716 |
if (!opt1.bValid || !opt2.bValid || !opt2.bFolder) { |
| 717 |
SetLastError(ERROR_TOMBO_E_INVALIDURI); |
| 718 |
return FALSE; |
| 719 |
} |
| 720 |
|
| 721 |
if (opt1.bFolder) { |
| 722 |
TString sSrcFull, sDstFull; |
| 723 |
if (!GetPhysicalPath(pCopyFrom, &sSrcFull)) return FALSE; |
| 724 |
if (!GetPhysicalPath(pCopyTo, &sDstFull)) return FALSE; |
| 725 |
|
| 726 |
if (IsSubFolder(sSrcFull.Get(), sDstFull.Get())) { |
| 727 |
SetLastError(ERROR_TOMBO_W_OPERATION_NOT_PERMITTED); |
| 728 |
return FALSE; |
| 729 |
} |
| 730 |
|
| 731 |
// Adjust Path |
| 732 |
TString sHL; |
| 733 |
if (!GetHeadLine(pCopyFrom, &sHL)) return FALSE; |
| 734 |
if (!sDstFull.StrCat(sHL.Get()) || !sDstFull.StrCat(TEXT("\\"))) return FALSE; |
| 735 |
|
| 736 |
MemoFolder mf; |
| 737 |
if (!mf.Init(pTopDir, sSrcFull.Get())) return FALSE; |
| 738 |
return mf.Copy(sDstFull.Get()); |
| 739 |
} else { |
| 740 |
if ((pOption->pNewHeadLine = new TString()) == NULL) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return FALSE; } |
| 741 |
if ((pOption->pNewURI = new TomboURI()) == NULL) { SetLastError(ERROR_NOT_ENOUGH_MEMORY); return FALSE; } |
| 742 |
|
| 743 |
TString sToPath; |
| 744 |
if (!GetPhysicalPath(pCopyTo, &sToPath)) return FALSE; |
| 745 |
|
| 746 |
if (opt1.bSafeFileName) { |
| 747 |
TString sOrigFull; |
| 748 |
if (!GetPhysicalPath(pCopyFrom, &sOrigFull)) return FALSE; |
| 749 |
|
| 750 |
TString sBase; |
| 751 |
if (!pCopyFrom->GetBaseName(&sBase)) return FALSE; |
| 752 |
TString sNewPath; |
| 753 |
if (!sNewPath.Join(sToPath.Get(), sBase.Get())) return FALSE; |
| 754 |
if (IsFileExist(sNewPath.Get())) { |
| 755 |
TString sNewBase; |
| 756 |
if (!GetSafeFileName(&sToPath, &sNewBase)) return FALSE; |
| 757 |
if (!sNewPath.Join(sToPath.Get(), sNewBase.Get())) return FALSE; |
| 758 |
if (!sBase.Set(sNewBase.Get())) return FALSE; |
| 759 |
} |
| 760 |
if (!CopyFile(sOrigFull.Get(), sNewPath.Get(), TRUE)) return FALSE; |
| 761 |
TString sOrigTDT, sNewTDT; |
| 762 |
if (sOrigTDT.Join(sOrigFull.Get(), TEXT(".tdt")) && |
| 763 |
sNewTDT.Join(sNewPath.Get(), TEXT(".tdt"))) { |
| 764 |
CopyFile(sOrigTDT.Get(), sNewTDT.Get(), TRUE); |
| 765 |
} |
| 766 |
if (!GetHeadLine(pCopyFrom, pOption->pNewHeadLine)) return FALSE; |
| 767 |
|
| 768 |
TString sNewURI; |
| 769 |
if (!sNewURI.Join(pCopyTo->GetFullURI(), sBase.Get())) return FALSE; |
| 770 |
if (!pOption->pNewURI->Init(sNewURI.Get())) return FALSE; |
| 771 |
|
| 772 |
return TRUE; |
| 773 |
} else { |
| 774 |
MemoNote *pNote = MemoNote::MemoNoteFactory(pCopyFrom); |
| 775 |
if (pNote == NULL) return FALSE; |
| 776 |
AutoPointer<MemoNote> ap(pNote); |
| 777 |
|
| 778 |
LPCTSTR pMemoPath = sToPath.Get() + _tcslen(pTopDir) + 1; |
| 779 |
|
| 780 |
MemoNote *pNewNote = MemoNote::CopyMemo(pTopDir, pNote, pMemoPath, pOption->pNewHeadLine); |
| 781 |
if (pNewNote == NULL) return FALSE; |
| 782 |
AutoPointer<MemoNote> ap2(pNewNote); |
| 783 |
|
| 784 |
pNewNote->GetURI(GetRepositoryName(), pOption->pNewURI); |
| 785 |
return TRUE; |
| 786 |
} |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
///////////////////////////////////////// |
| 791 |
// Move |
| 792 |
///////////////////////////////////////// |
| 793 |
|
| 794 |
BOOL LocalFileRepository::Move(const TomboURI *pMoveFrom, const TomboURI *pMoveTo, URIOption *pOption) |
| 795 |
{ |
| 796 |
if (!Copy(pMoveFrom, pMoveTo, pOption)) return FALSE; |
| 797 |
URIOption opt; |
| 798 |
if (!Delete(pMoveFrom, &opt)) return FALSE; |
| 799 |
return TRUE; |
| 800 |
} |
| 801 |
|
| 802 |
///////////////////////////////////////// |
| 803 |
// Rename headline |
| 804 |
///////////////////////////////////////// |
| 805 |
|
| 806 |
BOOL LocalFileRepository::ChangeHeadLine(const TomboURI *pURI, LPCTSTR pReqNewHeadLine, URIOption *pOption) |
| 807 |
{ |
| 808 |
URIOption opt(NOTE_OPTIONMASK_ENCRYPTED | NOTE_OPTIONMASK_SAFEFILE | NOTE_OPTIONMASK_VALID); |
| 809 |
if (!GetOption(pURI, &opt)) return FALSE; |
| 810 |
if (opt.bValid == FALSE) { |
| 811 |
SetLastError(ERROR_TOMBO_E_INVALIDURI); |
| 812 |
return FALSE; |
| 813 |
} |
| 814 |
|
| 815 |
if (opt.bFolder) { |
| 816 |
TString sFullPath; |
| 817 |
if (!GetPhysicalPath(pURI, &sFullPath)) return FALSE; |
| 818 |
|
| 819 |
MemoFolder mf; |
| 820 |
if (!mf.Init(pTopDir, sFullPath.Get())) return FALSE; |
| 821 |
|
| 822 |
return mf.Rename(pReqNewHeadLine); |
| 823 |
} else { |
| 824 |
if (opt.bEncrypt && opt.bSafeFileName) { |
| 825 |
SetLastError(ERROR_TOMBO_I_OPERATION_NOT_PERFORMED); |
| 826 |
return FALSE; |
| 827 |
} |
| 828 |
|
| 829 |
MemoNote *pNote = MemoNote::MemoNoteFactory(pURI); |
| 830 |
if (pNote == NULL) return FALSE; |
| 831 |
AutoPointer<MemoNote> ap(pNote); |
| 832 |
|
| 833 |
if (!pNote->Rename(pTopDir, pReqNewHeadLine)) return FALSE; |
| 834 |
|
| 835 |
TomboURI *p = new TomboURI(); |
| 836 |
if (p == NULL || !pNote->GetURI(GetRepositoryName(), p)) { |
| 837 |
delete p; |
| 838 |
return FALSE; |
| 839 |
} |
| 840 |
pOption->pNewURI = p; |
| 841 |
|
| 842 |
return TRUE; |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
///////////////////////////////////////// |
| 847 |
// GetList |
| 848 |
///////////////////////////////////////// |
| 849 |
|
| 850 |
DWORD LocalFileRepository::GetList(const TomboURI *pFolder, DirList *pList, BOOL bSkipEncrypt, BOOL bLooseDecrypt) |
| 851 |
{ |
| 852 |
TString sPartPath; |
| 853 |
if (!pFolder->GetFilePath(&sPartPath)) return TOMBO_REPO_GETLIST_FAIL; |
| 854 |
|
| 855 |
TString sFullPath; |
| 856 |
if (_tcslen(sPartPath.Get()) > 0) { |
| 857 |
if (!sFullPath.Join(pTopDir, TEXT("\\"), sPartPath.Get(), TEXT("*.*"))) return TOMBO_REPO_GETLIST_FAIL; |
| 858 |
} else { |
| 859 |
if (!sFullPath.Join(pTopDir, TEXT("\\*.*"))) return TOMBO_REPO_GETLIST_FAIL; |
| 860 |
} |
| 861 |
|
| 862 |
if (!pList->Init(pFolder->GetFullURI())) return TOMBO_REPO_GETLIST_FAIL; |
| 863 |
switch (pList->GetList(sFullPath.Get(), bSkipEncrypt, bLooseDecrypt)) { |
| 864 |
case DIRLIST_GETLIST_RESULT_SUCCESS: |
| 865 |
return TOMBO_REPO_GETLIST_SUCCESS; |
| 866 |
case DIRLIST_GETLIST_RESULT_PARTIAL: |
| 867 |
return TOMBO_REPO_GETLIST_PARTIAL; |
| 868 |
default: |
| 869 |
return TOMBO_REPO_GETLIST_FAIL; |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
URIList *LocalFileRepository::GetChild(const TomboURI *pFolder, BOOL bSkipEncrypt, BOOL bLooseDecrypt, BOOL *pLoose) |
| 874 |
{ |
| 875 |
URIList *pList = new URIList(); |
| 876 |
if (pList == NULL || !pList->Init()) { return FALSE; } |
| 877 |
|
| 878 |
DirList dlist; |
| 879 |
switch (GetList(pFolder, &dlist, bSkipEncrypt, bLooseDecrypt)) { |
| 880 |
case DIRLIST_GETLIST_RESULT_FAIL: |
| 881 |
return NULL; |
| 882 |
case DIRLIST_GETLIST_RESULT_SUCCESS: |
| 883 |
*pLoose = FALSE; |
| 884 |
break; |
| 885 |
case DIRLIST_GETLIST_RESULT_PARTIAL: |
| 886 |
*pLoose = TRUE; |
| 887 |
break; |
| 888 |
} |
| 889 |
|
| 890 |
for (DWORD i = 0; i < dlist.NumItems(); i++) { |
| 891 |
DirListItem *pItem = dlist.GetItem(i); |
| 892 |
LPCTSTR pFN = dlist.GetFileName(pItem->nFileNamePos); |
| 893 |
LPCTSTR pHL = dlist.GetFileName(pItem->nHeadLinePos); |
| 894 |
LPCTSTR pURI = dlist.GetFileName(pItem->nURIPos); |
| 895 |
|
| 896 |
TomboURI sURI; |
| 897 |
if (!sURI.Init(pURI)) return NULL; |
| 898 |
|
| 899 |
URIOption opt(NOTE_OPTIONMASK_VALID); |
| 900 |
if (!GetOption(&sURI, &opt)) return NULL; |
| 901 |
|
| 902 |
if (!opt.bFolder) { |
| 903 |
DWORD dirType = MemoNote::IsNote(pFN); |
| 904 |
if (dirType != NOTE_TYPE_PLAIN && dirType != NOTE_TYPE_CRYPTED) continue; |
| 905 |
} |
| 906 |
|
| 907 |
if (!pList->Add(&sURI, dlist.GetFileName(pItem->nHeadLinePos))) return NULL; |
| 908 |
} |
| 909 |
return pList; |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
///////////////////////////////////////// |
| 914 |
// |
| 915 |
///////////////////////////////////////// |
| 916 |
BOOL LocalFileRepository::RequestAllocateURI(const TomboURI *pBaseURI, LPCTSTR pText, TString *pHeadLine, TomboURI *pURI, const TomboURI *pTemplateURI) |
| 917 |
{ |
| 918 |
LPCTSTR pMemoPath; |
| 919 |
TString sMemoPath; |
| 920 |
if (!pBaseURI->GetFilePath(&sMemoPath)) return FALSE; |
| 921 |
pMemoPath = sMemoPath.Get(); |
| 922 |
|
| 923 |
MemoNote *pNote; |
| 924 |
if (pTemplateURI) { |
| 925 |
MemoNote *pCurrent = MemoNote::MemoNoteFactory(pTemplateURI); |
| 926 |
if (pCurrent == NULL) return FALSE; |
| 927 |
AutoPointer<MemoNote> apNote(pCurrent); |
| 928 |
|
| 929 |
pNote = pCurrent->GetNewInstance(); |
| 930 |
} else { |
| 931 |
pNote = new PlainMemoNote(); |
| 932 |
} |
| 933 |
|
| 934 |
if (pNote == NULL) return FALSE; |
| 935 |
|
| 936 |
AutoPointer<MemoNote> ap(pNote); |
| 937 |
|
| 938 |
TString sFullPath; |
| 939 |
TString sHeadLine; |
| 940 |
LPCTSTR pNotePath; |
| 941 |
|
| 942 |
if (!MemoNote::GetHeadLineFromMemoText(pText, &sHeadLine)) return FALSE; |
| 943 |
if (!MemoNote::GetHeadLinePath(pTopDir, pMemoPath, sHeadLine.Get(), |
| 944 |
pNote->GetExtension(), &sFullPath, &pNotePath, pHeadLine)) return FALSE; |
| 945 |
if (!pNote->Init(pNotePath)) return FALSE; |
| 946 |
|
| 947 |
if (!pNote->GetURI(GetRepositoryName(), pURI)) return FALSE; |
| 948 |
|
| 949 |
return TRUE; |
| 950 |
} |
| 951 |
|
| 952 |
BOOL LocalFileRepository::GetAttribute(const TomboURI *pURI, NoteAttribute *pAttribute) |
| 953 |
{ |
| 954 |
MemoNote *pNote = MemoNote::MemoNoteFactory(pURI); |
| 955 |
if (pNote == NULL) return FALSE; |
| 956 |
AutoPointer<MemoNote> ap(pNote); |
| 957 |
|
| 958 |
MemoInfo mi(pTopDir); |
| 959 |
DWORD nPos = 0; |
| 960 |
if (pNote->MemoPath()) { |
| 961 |
if (!mi.ReadInfo(pNote->MemoPath(), &nPos)) nPos = 0; |
| 962 |
} |
| 963 |
pAttribute->nCursorPos = nPos; |
| 964 |
|
| 965 |
BOOL bReadOnly; |
| 966 |
if (!g_Property.GetOpenReadOnly()) { |
| 967 |
// if (!pNote->IsReadOnly(&bReadOnly)) { |
| 968 |
// return FALSE; |
| 969 |
// } |
| 970 |
TString sFullPath; |
| 971 |
if (!sFullPath.Join(pTopDir, TEXT("\\"), pNote->MemoPath())) return FALSE; |
| 972 |
|
| 973 |
WIN32_FIND_DATA wfd; |
| 974 |
HANDLE h = FindFirstFile(sFullPath.Get(), &wfd); |
| 975 |
if (h == INVALID_HANDLE_VALUE) return FALSE; |
| 976 |
FindClose(h); |
| 977 |
|
| 978 |
bReadOnly = (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY; |
| 979 |
|
| 980 |
} else { |
| 981 |
bReadOnly = TRUE; |
| 982 |
} |
| 983 |
pAttribute->bReadOnly = bReadOnly; |
| 984 |
|
| 985 |
return TRUE; |
| 986 |
} |
| 987 |
|
| 988 |
BOOL LocalFileRepository::GetNoteAttribute(const TomboURI *pURI, UINT64 *pLastUpdate, UINT64 *pCreateDate, UINT64 *pFileSize) |
| 989 |
{ |
| 990 |
URIOption opt(NOTE_OPTIONMASK_VALID); |
| 991 |
if (!GetOption(pURI, &opt)) return FALSE; |
| 992 |
if (!opt.bValid) { SetLastError(ERROR_TOMBO_E_INVALIDURI); return FALSE; } |
| 993 |
if (opt.bFolder) { SetLastError(ERROR_NOT_SUPPORTED); return FALSE; } |
| 994 |
|
| 995 |
TString sFullPath; |
| 996 |
if (!GetPhysicalPath(pURI, &sFullPath)) return FALSE; |
| 997 |
|
| 998 |
WIN32_FIND_DATA wfd; |
| 999 |
HANDLE h = FindFirstFile(sFullPath.Get(), &wfd); |
| 1000 |
if (h != INVALID_HANDLE_VALUE) { |
| 1001 |
*pLastUpdate = ((UINT64)wfd.ftLastWriteTime.dwHighDateTime << 32) | (UINT64)wfd.ftLastWriteTime.dwLowDateTime ; |
| 1002 |
*pCreateDate = ((UINT64)wfd.ftCreationTime.dwHighDateTime << 32) | (UINT64)wfd.ftCreationTime.dwLowDateTime; |
| 1003 |
*pFileSize = ((UINT64)wfd.nFileSizeHigh << 32 ) | (UINT64)wfd.nFileSizeLow; |
| 1004 |
FindClose(h); |
| 1005 |
return TRUE; |
| 1006 |
} else { |
| 1007 |
return FALSE; |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
BOOL LocalFileRepository::SetAttribute(const TomboURI *pURI, const NoteAttribute *pAttribute) |
| 1012 |
{ |
| 1013 |
MemoNote *pNote = MemoNote::MemoNoteFactory(pURI); |
| 1014 |
if (pNote == NULL) return FALSE; |
| 1015 |
AutoPointer<MemoNote> ap(pNote); |
| 1016 |
|
| 1017 |
MemoInfo mi(pTopDir); |
| 1018 |
|
| 1019 |
if (pNote == NULL) return FALSE; |
| 1020 |
mi.WriteInfo(pNote->MemoPath(), pAttribute->nCursorPos); |
| 1021 |
|
| 1022 |
return TRUE; |
| 1023 |
} |
| 1024 |
|
| 1025 |
LPTSTR LocalFileRepository::GetNoteData(const TomboURI *pURI) |
| 1026 |
{ |
| 1027 |
MemoNote *pNote = MemoNote::MemoNoteFactory(pURI); |
| 1028 |
if (pNote == NULL) return FALSE; |
| 1029 |
AutoPointer<MemoNote> ap(pNote); |
| 1030 |
|
| 1031 |
BOOL bLoop = FALSE; |
| 1032 |
LPTSTR p; |
| 1033 |
|
| 1034 |
do { |
| 1035 |
bLoop = FALSE; |
| 1036 |
p = pNote->GetMemoBody(pTopDir, g_pPassManager); |
| 1037 |
if (p == NULL) { |
| 1038 |
DWORD nError = GetLastError(); |
| 1039 |
if (nError == ERROR_INVALID_PASSWORD) { |
| 1040 |
bLoop = TRUE; |
| 1041 |
} else { |
| 1042 |
return NULL; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
} while (bLoop); |
| 1046 |
return p; |
| 1047 |
} |
| 1048 |
|
| 1049 |
LPBYTE LocalFileRepository::GetNoteDataNative(const TomboURI *pURI, LPDWORD pSize) |
| 1050 |
{ |
| 1051 |
MemoNote *pNote = MemoNote::MemoNoteFactory(pURI); |
| 1052 |
if (pNote == NULL) return FALSE; |
| 1053 |
AutoPointer<MemoNote> ap(pNote); |
| 1054 |
|
| 1055 |
BOOL bLoop = FALSE; |
| 1056 |
LPBYTE p; |
| 1057 |
DWORD nSize; |
| 1058 |
do { |
| 1059 |
bLoop = FALSE; |
| 1060 |
p = pNote->GetMemoBodyNative(pTopDir, g_pPassManager, &nSize); |
| 1061 |
if (p == NULL) { |
| 1062 |
DWORD nError = GetLastError(); |
| 1063 |
if (nError == ERROR_INVALID_PASSWORD) { |
| 1064 |
bLoop = TRUE; |
| 1065 |
} else { |
| 1066 |
return NULL; |
| 1067 |
} |
| 1068 |
} |
| 1069 |
} while (bLoop); |
| 1070 |
|
| 1071 |
if (p) { |
| 1072 |
*pSize = nSize; |
| 1073 |
} |
| 1074 |
|
| 1075 |
return p; |
| 1076 |
} |
| 1077 |
|
| 1078 |
BOOL LocalFileRepository::ExecuteAssoc(const TomboURI *pURI, ExeAppType nType) |
| 1079 |
{ |
| 1080 |
URIOption opt(NOTE_OPTIONMASK_VALID); |
| 1081 |
if (!GetOption(pURI, &opt)) return FALSE; |
| 1082 |
if (opt.bValid == FALSE) { |
| 1083 |
SetLastError(ERROR_TOMBO_E_INVALIDURI); |
| 1084 |
return FALSE; |
| 1085 |
} |
| 1086 |
|
| 1087 |
if (opt.bFolder) { |
| 1088 |
if (nType != ExecType_Assoc) { |
| 1089 |
SetLastError(ERROR_NOT_SUPPORTED); |
| 1090 |
return FALSE; |
| 1091 |
} |
| 1092 |
|
| 1093 |
TString sCurrentPath; |
| 1094 |
if (!GetPhysicalPath(pURI, &sCurrentPath)) return FALSE; |
| 1095 |
|
| 1096 |
#if defined(PLATFORM_PKTPC) || defined(PLATFORM_WM5) |
| 1097 |
STARTUPINFO si; |
| 1098 |
PROCESS_INFORMATION pi; |
| 1099 |
memset(&si, 0, sizeof(si)); |
| 1100 |
memset(&pi, 0, sizeof(pi)); |
| 1101 |
si.cb = sizeof(si); |
| 1102 |
|
| 1103 |
if (!CreateProcess(TEXT("\\windows\\iexplore.exe"), sCurrentPath.Get(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE; |
| 1104 |
CloseHandle(pi.hProcess); |
| 1105 |
CloseHandle(pi.hThread); |
| 1106 |
return TRUE; |
| 1107 |
#else |
| 1108 |
SHELLEXECUTEINFO se; |
| 1109 |
memset(&se, 0, sizeof(se)); |
| 1110 |
se.cbSize = sizeof(se); |
| 1111 |
se.hwnd = NULL; |
| 1112 |
se.lpVerb = TEXT("explore"); |
| 1113 |
se.lpFile = sCurrentPath.Get(); |
| 1114 |
se.lpParameters = NULL; |
| 1115 |
se.lpDirectory = NULL; |
| 1116 |
se.nShow = SW_SHOWNORMAL; |
| 1117 |
ShellExecuteEx(&se); |
| 1118 |
return TRUE; |
| 1119 |
#endif |
| 1120 |
} else { |
| 1121 |
TString sFullPath; |
| 1122 |
if (!GetPhysicalPath(pURI, &sFullPath)) return FALSE; |
| 1123 |
|
| 1124 |
if (nType == ExecType_Assoc) { |
| 1125 |
SHELLEXECUTEINFO se; |
| 1126 |
memset(&se, 0, sizeof(se)); |
| 1127 |
se.cbSize = sizeof(se); |
| 1128 |
se.hwnd = NULL; |
| 1129 |
se.lpVerb = TEXT("open"); |
| 1130 |
se.lpFile = sFullPath.Get(); |
| 1131 |
se.lpParameters = NULL; |
| 1132 |
se.lpDirectory = NULL; |
| 1133 |
se.nShow = SW_SHOWNORMAL; |
| 1134 |
ShellExecuteEx(&se); |
| 1135 |
if ((int)se.hInstApp < 32) return FALSE; |
| 1136 |
return TRUE; |
| 1137 |
} else if (nType == ExecType_ExtApp1 || nType == ExecType_ExtApp2) { |
| 1138 |
LPCTSTR pExeFile = nType == ExecType_ExtApp1 ? g_Property.GetExtApp1() : g_Property.GetExtApp2(); |
| 1139 |
STARTUPINFO si; |
| 1140 |
PROCESS_INFORMATION pi; |
| 1141 |
memset(&si, 0, sizeof(si)); |
| 1142 |
memset(&pi, 0, sizeof(pi)); |
| 1143 |
si.cb = sizeof(si); |
| 1144 |
|
| 1145 |
TString sExe; |
| 1146 |
TString sCmdLine; |
| 1147 |
#if defined(PLATFORM_WIN32) |
| 1148 |
if (!sCmdLine.Join(TEXT("\""), pExeFile, TEXT("\" "))) return FALSE; |
| 1149 |
if (!sCmdLine.StrCat(TEXT("\""))) return FALSE; |
| 1150 |
if (!sCmdLine.StrCat(sFullPath.Get())) return FALSE; |
| 1151 |
if (!sCmdLine.StrCat(TEXT("\""))) return FALSE; |
| 1152 |
if (!CreateProcess(NULL, sCmdLine.Get(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE; |
| 1153 |
CloseHandle(pi.hProcess); |
| 1154 |
CloseHandle(pi.hThread); |
| 1155 |
return TRUE; |
| 1156 |
|
| 1157 |
#endif |
| 1158 |
#if defined(PLATFORM_HPC) |
| 1159 |
if (!sExe.Set(pExeFile)) return FALSE; |
| 1160 |
if (!sCmdLine.Join(TEXT("\""), sFullPath.Get(), TEXT("\""))) return FALSE; |
| 1161 |
if (!CreateProcess(sExe.Get(), sCmdLine.Get(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE; |
| 1162 |
CloseHandle(pi.hProcess); |
| 1163 |
CloseHandle(pi.hThread); |
| 1164 |
return TRUE; |
| 1165 |
|
| 1166 |
#endif |
| 1167 |
#if defined(PLATFORM_PKTPC) || defined(PLATFORM_WM5) |
| 1168 |
if (!sExe.Set(pExeFile)) return FALSE; |
| 1169 |
if (!sCmdLine.Set(sFullPath.Get())) return FALSE; |
| 1170 |
if (!CreateProcess(sExe.Get(), sCmdLine.Get(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return FALSE; |
| 1171 |
CloseHandle(pi.hProcess); |
| 1172 |
CloseHandle(pi.hThread); |
| 1173 |
return TRUE; |
| 1174 |
#endif |
| 1175 |
#if defined(PLATFORM_BE500) |
| 1176 |
return CoshExecute(NULL, pExeFile, sFullPath.Get()); |
| 1177 |
#endif |
| 1178 |
} else { |
| 1179 |
SetLastError(ERROR_NOT_SUPPORTED); |
| 1180 |
return FALSE; |
| 1181 |
} |
| 1182 |
} |
| 1183 |
} |
| 1184 |
|
| 1185 |
BOOL LocalFileRepository::MakeFolder(const TomboURI *pURI, LPCTSTR pFolderName) |
| 1186 |
{ |
| 1187 |
URIOption opt(NOTE_OPTIONMASK_VALID); |
| 1188 |
if (!GetOption(pURI, &opt)) return FALSE; |
| 1189 |
if (!opt.bValid) { SetLastError(ERROR_TOMBO_E_INVALIDURI); return FALSE; } |
| 1190 |
if (!opt.bFolder) { SetLastError(ERROR_NOT_SUPPORTED); return FALSE; } |
| 1191 |
|
| 1192 |
TString sPath; |
| 1193 |
if (!GetPhysicalPath(pURI, &sPath)) return FALSE; |
| 1194 |
if (!sPath.StrCat(pFolderName)) return FALSE; |
| 1195 |
TrimRight(sPath.Get()); |
| 1196 |
ChopFileSeparator(sPath.Get()); |
| 1197 |
return CreateDirectory(sPath.Get(), NULL); |
| 1198 |
} |
| 1199 |
|
| 1200 |
///////////////////////////////////////// |
| 1201 |
// Virtual folder |
| 1202 |
///////////////////////////////////////// |
| 1203 |
|
| 1204 |
VFolderRepository::VFolderRepository() |
| 1205 |
{ |
| 1206 |
} |
| 1207 |
|
| 1208 |
VFolderRepository::~VFolderRepository() |
| 1209 |
{ |
| 1210 |
} |
| 1211 |
|
| 1212 |
BOOL VFolderRepository::Init(LPCTSTR pRepName, LPCTSTR pDispName) |
| 1213 |
{ |
| 1214 |
return RepositoryImpl::Init(pRepName, pDispName, TOMBO_REPO_SUBREPO_TYPE_VFOLDER); |
| 1215 |
} |
| 1216 |
|
| 1217 |
RepositoryImpl *VFolderRepository::Clone() |
| 1218 |
{ |
| 1219 |
VFolderRepository *pImpl = new VFolderRepository(); |
| 1220 |
if (pImpl == NULL) { |
| 1221 |
SetLastError(ERROR_NOT_ENOUGH_MEMORY); |
| 1222 |
return NULL; |
| 1223 |
} |
| 1224 |
if (!pImpl->Init(GetRepositoryName(), GetDisplayName())) { |
| 1225 |
return NULL; |
| 1226 |
} |
| 1227 |
return pImpl; |
| 1228 |
} |
| 1229 |
|
| 1230 |
LPTSTR VFolderRepository::GetXMLSaveString() |
| 1231 |
{ |
| 1232 |
TString sXMLStr; |
| 1233 |
if (!sXMLStr.Join(TEXT(" <vfolder name=\""), GetRepositoryName(), TEXT("\" dispname=\""), GetDisplayName())) return NULL; |
| 1234 |
if (!sXMLStr.StrCat(TEXT("\"/>\n"))) return NULL; |
| 1235 |
return StringDup(sXMLStr.Get()); |
| 1236 |
} |
| 1237 |
|
| 1238 |
BOOL VFolderRepository::Update(const TomboURI *pCurrentURI, LPCTSTR pData, TomboURI *pNewURI, TString *pNewHeadLine){ return FALSE; } |
| 1239 |
BOOL VFolderRepository::Delete(const TomboURI *pURI, URIOption *pOption){ return FALSE; } |
| 1240 |
BOOL VFolderRepository::Copy(const TomboURI *pCopyFrom, const TomboURI *pCopyTo, URIOption *pOption){ return FALSE; } |
| 1241 |
BOOL VFolderRepository::Move(const TomboURI *pMoveFrom, const TomboURI *pMoveTo, URIOption *pOption){ return FALSE; } |
| 1242 |
|
| 1243 |
BOOL VFolderRepository::ChangeHeadLine(const TomboURI *pURI, LPCTSTR pReqNewHeadLine, URIOption *pOption){ return FALSE; } |
| 1244 |
|
| 1245 |
BOOL VFolderRepository::GetHeadLine(const TomboURI *pURI, TString *pHeadLine){ return FALSE; } |
| 1246 |
|
| 1247 |
BOOL VFolderRepository::GetOption(const TomboURI *pURI, URIOption *pOption) const{ return FALSE; } |
| 1248 |
BOOL VFolderRepository::SetOption(const TomboURI *pCurrentURI, URIOption *pOption){ return FALSE; } |
| 1249 |
|
| 1250 |
BOOL VFolderRepository::GetPhysicalPath(const TomboURI *pURI, TString *pFullPath){ return FALSE; } |
| 1251 |
|
| 1252 |
URIList *VFolderRepository::GetChild(const TomboURI *pFolder, BOOL bSkipEncrypt, BOOL bLooseDecrypt, BOOL *pLoose) { return NULL; } |
| 1253 |
|
| 1254 |
BOOL VFolderRepository::RequestAllocateURI(const TomboURI *pBaseURI, LPCTSTR pText, TString *pHeadLine, TomboURI *pURI, const TomboURI *pTemplateURI){ return FALSE; } |
| 1255 |
|
| 1256 |
BOOL VFolderRepository::GetAttribute(const TomboURI *pURI, NoteAttribute *pAttribute){ return FALSE; } |
| 1257 |
BOOL VFolderRepository::SetAttribute(const TomboURI *pURI, const NoteAttribute *pAttribute){ return FALSE; } |
| 1258 |
BOOL VFolderRepository::GetNoteAttribute(const TomboURI *pURI, UINT64 *pLastUpdate, UINT64 *pCreateDate, UINT64 *pFileSize){ return FALSE; } |
| 1259 |
|
| 1260 |
LPTSTR VFolderRepository::GetNoteData(const TomboURI *pURI) { return NULL; } |
| 1261 |
LPBYTE VFolderRepository::GetNoteDataNative(const TomboURI *pURI, LPDWORD pSize) { return NULL; } |
| 1262 |
|
| 1263 |
BOOL VFolderRepository::ExecuteAssoc(const TomboURI *pURI, ExeAppType nType){ return FALSE; } |
| 1264 |
BOOL VFolderRepository::MakeFolder(const TomboURI *pURI, LPCTSTR pFolderName){ return FALSE; } |
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|