UIFinderWindow.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. //
  2. // Copyright (c) 2014-2017, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_widgets.h>
  23. #include <TurboBadger/tb_widgets_common.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/IO/Log.h>
  26. #include <Atomic/IO/File.h>
  27. #include "UI.h"
  28. #include "UIEvents.h"
  29. #include "UIWindow.h"
  30. #include "UIEditField.h"
  31. #include "UISelectList.h"
  32. #include "UIPromptWindow.h"
  33. #include "UIFinderWindow.h"
  34. #include "UISelectItem.h"
  35. #include "UIMenuWindow.h"
  36. using namespace tb;
  37. namespace Atomic
  38. {
  39. /// finder window
  40. UIFinderWindow::UIFinderWindow(Context* context, UIWidget* target, const String& id, bool createWidget) :
  41. UIWindow(context, false),
  42. finderMode_(0),
  43. bookmarksDirty_(0)
  44. {
  45. if (createWidget)
  46. {
  47. widget_ = new TBFinderWindow(target ? target->GetInternalWidget() : 0, TBIDC(id.CString()));
  48. widget_->SetDelegate(this);
  49. GetSubsystem<UI>()->WrapWidget(this, widget_);
  50. }
  51. }
  52. UIFinderWindow::~UIFinderWindow()
  53. {
  54. SaveBookmarks();
  55. if ( !newFolderPtr_.Expired())
  56. newFolderPtr_->UnsubscribeFromAllEvents();
  57. if ( !newBookmarkPtr_.Expired() )
  58. newBookmarkPtr_->UnsubscribeFromAllEvents();
  59. }
  60. void UIFinderWindow::FindFile(const String& title, const String& preset, int dimmer, int width, int height)
  61. {
  62. if (!widget_)
  63. return;
  64. if ( ((TBFinderWindow*)widget_)->Show(title.CString(), preset.CString(), dimmer, width, height) )
  65. {
  66. CreateBookmarks();
  67. PresetCurrentPath(preset);
  68. UpdateUiPath();
  69. UpdateUiList();
  70. UpdateUiResult();
  71. }
  72. }
  73. void UIFinderWindow::FindPath(const String& title, const String& preset, int dimmer, int width, int height)
  74. {
  75. if (!widget_)
  76. return;
  77. finderMode_ = 1;
  78. if ( ((TBFinderWindow*)widget_)->Show(title.CString(), preset.CString(), dimmer, width, height) )
  79. {
  80. UIWidget *reswid = GetResultWidget();
  81. if (reswid) reswid->SetVisibility( UI_WIDGET_VISIBILITY_INVISIBLE );
  82. CreateBookmarks();
  83. PresetCurrentPath(preset);
  84. UpdateUiPath();
  85. UpdateUiList();
  86. UpdateUiResult();
  87. }
  88. }
  89. bool UIFinderWindow::OnEvent(const tb::TBWidgetEvent &ev)
  90. {
  91. if ( ev.type == EVENT_TYPE_CHANGED && ev.target && ((uint32)ev.target->GetID()) == UIFINDEREDITPATHID )
  92. {
  93. FileSystem* filesystem = GetSubsystem<FileSystem>();
  94. UIWidget *pathwidget = GetPathWidget(); // paste or type in currentpath widget
  95. if(pathwidget)
  96. {
  97. if ( filesystem->DirExists (pathwidget->GetText()))
  98. {
  99. if ( pathwidget->GetText() != currentPath_ )
  100. {
  101. resultPath_ = "";
  102. SetCurrentPath (pathwidget->GetText());
  103. UpdateUiList();
  104. UpdateUiResult();
  105. }
  106. }
  107. }
  108. return true;
  109. }
  110. if ( ev.type == EVENT_TYPE_POINTER_UP && ev.target && ((uint32)ev.target->GetID()) == UIFINDERUPBUTTONID ) // go up
  111. {
  112. GoFolderUp();
  113. return true;
  114. }
  115. if ( ev.type == EVENT_TYPE_POINTER_UP && ev.target && ((uint32)ev.target->GetID()) == UIFINDERBOOKBUTTONID ) // add bookmark request
  116. {
  117. // this check is necessary because you can kill the bookmark window with the "X" and
  118. // this kills the UI but not the newBookmarkPtr to it, and it doesnt get cleaned up properly.
  119. if ( newBookmarkPtr_.NotNull() )
  120. {
  121. newBookmarkPtr_->UnsubscribeFromAllEvents();
  122. newBookmarkPtr_.Reset();
  123. }
  124. newBookmarkPtr_ = new UIPromptWindow(context_, this, "createbookmark", true);
  125. SubscribeToEvent(newBookmarkPtr_, E_UIPROMPTCOMPLETE, ATOMIC_HANDLER(UIFinderWindow, HandleCreateBookmark ));
  126. String prospect = "";
  127. char delim = '/';
  128. Vector <String> tokens = currentPath_.Split(delim, false);
  129. prospect = tokens[ tokens.Size()-1 ]; // get the last folder name as preset
  130. newBookmarkPtr_->Show("Create New Bookmark", "Enter a name for the new bookmark", prospect );
  131. return true;
  132. }
  133. if ( ev.type == EVENT_TYPE_POINTER_UP && ev.target && ((uint32)ev.target->GetID()) == UIFINDERFOLDERBUTTONID ) // add folder request
  134. {
  135. if (newFolderPtr_.NotNull())
  136. {
  137. newFolderPtr_->UnsubscribeFromAllEvents();
  138. newFolderPtr_.Reset();
  139. }
  140. newFolderPtr_ = new UIPromptWindow(context_, this, "createfolder", true);
  141. SubscribeToEvent(newFolderPtr_, E_UIPROMPTCOMPLETE, ATOMIC_HANDLER(UIFinderWindow, HandleCreateFolder));
  142. newFolderPtr_->Show("Create new folder", "Enter a name for the new folder", "" );
  143. return true;
  144. }
  145. if ( ev.type == EVENT_TYPE_CLICK && ev.target && ((uint32)ev.target->GetID()) == 5 ) // clicked in bookmarks
  146. {
  147. UISelectList *bklist = static_cast<UISelectList *>(GetBookmarksWidget());
  148. int selected = bklist->GetValue();
  149. if ( selected >= 0 )
  150. {
  151. resultPath_ = ""; // were going back, give up file.
  152. SetCurrentPath ( bookmarkPaths_[selected]);
  153. UpdateUiPath();
  154. UpdateUiList();
  155. UpdateUiResult();
  156. bklist->SetValue(-1); // clear the select visuals
  157. }
  158. return true;
  159. }
  160. if ( ev.type == EVENT_TYPE_CUSTOM && ev.target && ((uint32)ev.target->GetID()) == UIFINDERBOOKLISTID ) // bookmarks TB context menu result
  161. {
  162. UI* ui = GetSubsystem<UI>();
  163. if ( ev.special_key == tb::TB_KEY_DELETE ) // we wanna delete something
  164. {
  165. String myid;
  166. ui->GetTBIDString(ev.target?((uint32)ev.target->GetID()) : 0, myid);
  167. UISelectList *bklist = static_cast<UISelectList *>(GetBookmarksWidget());
  168. if (bklist)
  169. {
  170. int myindex = bklist->FindId ( (uint32)ev.ref_id );
  171. if ( myindex >= 0 )
  172. {
  173. DeleteBookmark(myindex);
  174. }
  175. }
  176. }
  177. return true;
  178. }
  179. if ( ev.type == EVENT_TYPE_CLICK && ev.target && ((uint32)ev.target->GetID()) == UIFINDERFILELISTID ) // clicked dirfiles list
  180. {
  181. UISelectList *filelist = static_cast<UISelectList *>(GetFilesWidget());
  182. ComposePath( filelist->GetSelectedItemString() );
  183. return true;
  184. }
  185. if ( ev.type == EVENT_TYPE_CLICK && (ev.ref_id == UIFINDEROKBUTTONID|| ev.ref_id == UIFINDERCANCELBUTTONID ) ) // clicked ok or cancel button
  186. {
  187. UI* ui = GetSubsystem<UI>();
  188. VariantMap eventData;
  189. String title = "FinderWindow";
  190. TBStr tbtext;
  191. if( widget_ && (TBWindow*)widget_->GetText(tbtext) )
  192. title = tbtext.CStr();
  193. eventData[UIFinderComplete::P_TITLE] = title;
  194. eventData[UIFinderComplete::P_SELECTED] = "";
  195. eventData[UIFinderComplete::P_REASON] = "CANCEL";
  196. if (ev.ref_id == UIFINDEROKBUTTONID) // ok button was pressed, otherwise it was cancel button
  197. {
  198. eventData[UIFinderComplete::P_REASON] = "OK";
  199. if ( finderMode_ == 0 ) // finding a file
  200. { // get from widget, in case the user had been typing.
  201. UIWidget *ewidget = GetResultWidget();
  202. if( ewidget) eventData[UIFinderComplete::P_SELECTED] = ewidget->GetText();
  203. }
  204. else // finding a folder
  205. {
  206. UIWidget *cwidget = GetPathWidget();
  207. if( cwidget) eventData[UIFinderComplete::P_SELECTED] = cwidget->GetText();
  208. }
  209. }
  210. ConvertEvent(this, ui->WrapWidget(ev.target), ev, eventData);
  211. SendEvent(E_UIFINDERCOMPLETE, eventData);
  212. if (eventData[WidgetEvent::P_HANDLED].GetBool())
  213. return true;
  214. }
  215. return UIWindow::OnEvent(ev);
  216. }
  217. void UIFinderWindow::HandleCreateBookmark(StringHash eventType, VariantMap& eventData)
  218. {
  219. const String& Title = eventData["Title"].GetString();
  220. const String& Reason = eventData["Reason"].GetString();
  221. const String& Selected = eventData["Selected"].GetString();
  222. if( Reason == "OK" )
  223. CreateBookmark( Selected, currentPath_ );
  224. if (newBookmarkPtr_)
  225. {
  226. newBookmarkPtr_->UnsubscribeFromAllEvents();
  227. newBookmarkPtr_.Reset();
  228. }
  229. }
  230. void UIFinderWindow::HandleCreateFolder(StringHash eventType, VariantMap& eventData)
  231. {
  232. const String& Title = eventData["Title"].GetString();
  233. const String& Reason = eventData["Reason"].GetString();
  234. const String& Selected = eventData["Selected"].GetString();
  235. if( Reason == "OK" )
  236. CreateFolder(Selected);
  237. if (newFolderPtr_)
  238. {
  239. newFolderPtr_->UnsubscribeFromAllEvents();
  240. newFolderPtr_.Reset();
  241. }
  242. }
  243. UIWidget* UIFinderWindow::GetWindowWidget()
  244. {
  245. if (!widget_)
  246. return 0;
  247. UI* ui = GetSubsystem<UI>();
  248. return ui->WrapWidget(widget_);
  249. }
  250. UIWidget* UIFinderWindow::GetPathWidget()
  251. {
  252. if (!widget_)
  253. return 0;
  254. TBWidget* child = (TBWidget*) widget_->GetWidgetByIDAndType<TBEditField>(UIFINDEREDITPATHID);
  255. if (!child)
  256. return 0;
  257. UI* ui = GetSubsystem<UI>();
  258. return ui->WrapWidget(child);
  259. }
  260. UIWidget* UIFinderWindow::GetResultWidget()
  261. {
  262. if (!widget_)
  263. return 0;
  264. TBWidget* child = (TBWidget*)widget_->GetWidgetByIDAndType<TBEditField>(UIFINDEREDITFILEID);
  265. if (!child)
  266. return 0;
  267. UI* ui = GetSubsystem<UI>();
  268. return ui->WrapWidget(child);
  269. }
  270. UIWidget* UIFinderWindow::GetBookmarksWidget()
  271. {
  272. if (!widget_)
  273. return 0;
  274. TBWidget* child = (TBWidget*)widget_->GetWidgetByIDAndType<TBSelectList>(UIFINDERBOOKLISTID);
  275. if (!child)
  276. return 0;
  277. UI* ui = GetSubsystem<UI>();
  278. return ui->WrapWidget(child);
  279. }
  280. UIWidget* UIFinderWindow::GetFilesWidget()
  281. {
  282. if (!widget_)
  283. return 0;
  284. TBWidget* child = (TBWidget*)widget_->GetWidgetByIDAndType<TBSelectList>(UIFINDERFILELISTID);
  285. if (!child)
  286. return 0;
  287. UI* ui = GetSubsystem<UI>();
  288. return ui->WrapWidget(child);
  289. }
  290. // where the finder starts
  291. void UIFinderWindow::PresetCurrentPath( const String& preset )
  292. {
  293. FileSystem* filesystem = GetSubsystem<FileSystem>();
  294. if ( !preset.Empty() && filesystem->DirExists (preset) )
  295. SetCurrentPath (preset);
  296. else
  297. SetCurrentPath ( filesystem->GetUserDocumentsDir() );
  298. }
  299. // set the current path value
  300. void UIFinderWindow::SetCurrentPath( const String& string )
  301. {
  302. currentPath_ = string;
  303. }
  304. //using the list, jam things together, we'll either get another path or a file.
  305. void UIFinderWindow::ComposePath (const String& string )
  306. {
  307. String prospect = currentPath_ + string;
  308. FileSystem* filesystem = GetSubsystem<FileSystem>();
  309. if ( !filesystem->FileExists ( prospect ) ) // its a dir, feel the joy
  310. {
  311. SetCurrentPath( prospect + "/" ); // add the trailing slash, OR ELSE
  312. UpdateUiPath();
  313. UpdateUiList();
  314. UpdateUiResult();
  315. }
  316. else // its a file
  317. {
  318. resultPath_ = prospect;
  319. UpdateUiResult();
  320. }
  321. }
  322. // create the list of bookmarks ... can be different per platform
  323. void UIFinderWindow::CreateBookmarks()
  324. {
  325. FileSystem* filesystem = GetSubsystem<FileSystem>();
  326. String basepath = filesystem->GetUserDocumentsDir();
  327. UISelectList *bklist = static_cast<UISelectList *>(GetBookmarksWidget());
  328. #if defined(ATOMIC_PLATFORM_LINUX)
  329. if ( filesystem->DirExists ( basepath )) CreateBookmark ( "Home", basepath );
  330. if ( filesystem->DirExists ( basepath + "Documents")) CreateBookmark ( "Documents", basepath + "Documents/" );
  331. if ( filesystem->DirExists ( basepath + "Music")) CreateBookmark ( "Music", basepath + "Music/" );
  332. if ( filesystem->DirExists ( basepath + "Pictures" )) CreateBookmark ( "Pictures", basepath + "Pictures/" );
  333. if ( filesystem->DirExists ( basepath + "Videos" )) CreateBookmark ( "Videos", basepath + "Videos/" );
  334. if ( filesystem->DirExists ( basepath + "Downloads")) CreateBookmark ( "Downloads", basepath + "Downloads/" );
  335. #elif defined(ATOMIC_PLATFORM_WINDOWS)
  336. if ( filesystem->DirExists ( basepath )) CreateBookmark ( "Home", basepath );
  337. if ( filesystem->DirExists ( basepath + "Desktop")) CreateBookmark ( "Desktop", basepath + "Desktop/" );
  338. if ( filesystem->DirExists ( basepath + "Documents")) CreateBookmark ( "Documents", basepath + "Documents/" );
  339. if ( filesystem->DirExists ( basepath + "Downloads")) CreateBookmark ( "Downloads", basepath + "Downloads/" );
  340. if ( filesystem->DirExists ( basepath + "Music")) CreateBookmark ( "Music", basepath + "Music/" );
  341. if ( filesystem->DirExists ( basepath + "Pictures" )) CreateBookmark ( "Pictures", basepath + "Pictures/" );
  342. if ( filesystem->DirExists ( basepath + "Videos" )) CreateBookmark ( "Videos", basepath + "Videos/" );
  343. #elif defined(ATOMIC_PLATFORM_OSX)
  344. if ( filesystem->DirExists ( basepath )) CreateBookmark ( "Home", basepath );
  345. if ( filesystem->DirExists ( basepath + "Documents")) CreateBookmark ( "Documents", basepath + "Documents/" );
  346. if ( filesystem->DirExists ( basepath + "Downloads")) CreateBookmark ( "Downloads", basepath + "Downloads/" );
  347. if ( filesystem->DirExists ( basepath + "Public")) CreateBookmark ( "Public", basepath + "Public/" );
  348. #else // android, ios, web?
  349. if ( filesystem->DirExists ( basepath )) CreateBookmark ( "Home", basepath );
  350. #endif
  351. CreateBookmark ( "-", basepath ); // create separator!
  352. LoadBookmarks();
  353. bklist->SetValue(-1); // fix the selection and scrolling
  354. }
  355. // go up tree 1 folder
  356. void UIFinderWindow::GoFolderUp()
  357. {
  358. String prospect = "";
  359. char delim = '/';
  360. Vector <String> tokens = currentPath_.Split(delim, false);
  361. if ( tokens.Size() == 0 ) // were at the top
  362. prospect = "/";
  363. else
  364. {
  365. int nn = 0;
  366. for ( nn=0; nn<tokens.Size()-1; nn++ )
  367. {
  368. prospect += delim;
  369. prospect += tokens[nn];
  370. }
  371. prospect += delim;
  372. }
  373. if ( prospect != currentPath_ )
  374. {
  375. resultPath_ = "";
  376. SetCurrentPath (prospect);
  377. UpdateUiPath();
  378. UpdateUiList();
  379. UpdateUiResult();
  380. }
  381. }
  382. // move current path to widget
  383. void UIFinderWindow::UpdateUiPath ()
  384. {
  385. UIWidget *pathwidget = GetPathWidget();
  386. if(pathwidget)
  387. {
  388. if ( pathwidget->GetText() != currentPath_ )
  389. pathwidget->SetText(currentPath_);
  390. }
  391. }
  392. // move result path to widget
  393. void UIFinderWindow::UpdateUiResult ()
  394. {
  395. UIWidget *resultwidget = GetResultWidget();
  396. if( resultwidget)
  397. {
  398. if ( resultwidget->GetText() != resultPath_ )
  399. resultwidget->SetText(resultPath_);
  400. }
  401. }
  402. #include "Container/Sort.h"
  403. // a very local compare function
  404. bool CompareStrs(const String& a, const String &b)
  405. {
  406. return a < b;
  407. }
  408. // move folder contents into list
  409. void UIFinderWindow::UpdateUiList()
  410. {
  411. FileSystem* filesystem = GetSubsystem<FileSystem>();
  412. UISelectList *filelist = static_cast<UISelectList *>(GetFilesWidget());
  413. UISelectItemSource *fileSource = new UISelectItemSource(context_);
  414. if ( filesystem->DirExists (currentPath_ ) )
  415. {
  416. Vector <String> mydirs;
  417. int nn = 0;
  418. filesystem->ScanDir (mydirs,currentPath_, "*", SCAN_DIRS, false );
  419. Sort(mydirs.Begin(), mydirs.End(), CompareStrs); // sort them
  420. for ( nn=0; nn<mydirs.Size(); nn++ )
  421. {
  422. if ( mydirs[nn] == "." ) continue;
  423. if ( mydirs[nn] == ".." ) continue;
  424. String idz = "DIR"+ String(nn);
  425. fileSource->AddItem( new UISelectItem(context_, mydirs[nn], idz, "FolderIcon" ));
  426. }
  427. Vector <String> myfiles;
  428. filesystem->ScanDir (myfiles, currentPath_, "*", SCAN_FILES, false );
  429. Sort(myfiles.Begin(), myfiles.End(), CompareStrs);
  430. for ( nn=0; nn<myfiles.Size(); nn++ )
  431. {
  432. String idz = "FIL"+ String(nn);
  433. fileSource->AddItem(new UISelectItem( context_, myfiles[nn], idz));
  434. }
  435. }
  436. filelist->SetValue(-1);
  437. filelist->SetSource(fileSource);
  438. }
  439. // utility to add a folder in current path
  440. void UIFinderWindow::CreateFolder( const String& string )
  441. {
  442. FileSystem* filesystem = GetSubsystem<FileSystem>();
  443. if ( filesystem->CreateDir( currentPath_ + string ) )
  444. {
  445. UpdateUiList();
  446. }
  447. }
  448. // utility to add a bookmark from the current path
  449. void UIFinderWindow::CreateBookmark ( const String& bkname, const String& bkpath )
  450. {
  451. UISelectList *bklist = static_cast<UISelectList *>(GetBookmarksWidget());
  452. if (bklist )
  453. {
  454. int inspos = bklist->GetNumItems();
  455. if ( inspos < 0 ) inspos = 0;
  456. String idz = "BKM" + String(inspos);
  457. if ( bklist->AddItem ( inspos, bkname, idz ) )
  458. {
  459. bookmarks_.Push(bkname);
  460. bookmarkPaths_.Push (bkpath);
  461. bookmarksDirty_ = 1;
  462. }
  463. }
  464. }
  465. // removes a bookmark based upon its index in the bookmark list
  466. void UIFinderWindow::DeleteBookmark ( int bkindex )
  467. {
  468. UISelectList *bklist = static_cast<UISelectList *>(GetBookmarksWidget());
  469. if (bklist && bkindex >= 0)
  470. {
  471. bklist->DeleteItem(bkindex);
  472. bookmarks_.Erase(bkindex,1);
  473. bookmarkPaths_.Erase(bkindex,1);
  474. resultPath_ = "";
  475. SetCurrentPath (bookmarkPaths_[0]);
  476. UpdateUiPath();
  477. UpdateUiList();
  478. UpdateUiResult();
  479. bklist->SetValue(-1);
  480. bookmarksDirty_ = 1;
  481. }
  482. }
  483. void UIFinderWindow::LoadBookmarks()
  484. {
  485. FileSystem* filesystem = GetSubsystem<FileSystem>();
  486. String bkdata = "";
  487. String bkpath = "";
  488. #if defined(ATOMIC_PLATFORM_ANDROID) || defined(ATOMIC_PLATFORM_IOS)
  489. bkpath = filesystem->GetUserDocumentsDir(); // somewhere writable on mobile
  490. #else
  491. bkpath = filesystem->GetAppPreferencesDir("AtomicGameEngine", "Bookmarks"); // desktop systems
  492. #endif
  493. bkpath += "/Bookmarks.txt";
  494. if ( filesystem->FileExists ( bkpath ) )
  495. {
  496. File *fp = new File (context_, bkpath, FILE_READ);
  497. if(fp->IsOpen())
  498. {
  499. fp->ReadText(bkdata);
  500. fp->Close();
  501. }
  502. }
  503. char delim = '\n';
  504. Vector <String> tokens = bkdata.Split(delim, false);
  505. int nn=0;
  506. for ( nn=0; nn< tokens.Size(); nn+=2)
  507. {
  508. CreateBookmark ( tokens[nn], tokens[nn+1] );
  509. }
  510. bookmarksDirty_ = 0;
  511. }
  512. void UIFinderWindow::SaveBookmarks()
  513. {
  514. if ( bookmarksDirty_ > 0 )
  515. {
  516. FileSystem* filesystem = GetSubsystem<FileSystem>();
  517. String bkpath = "";
  518. #if defined(ATOMIC_PLATFORM_ANDROID) || defined(ATOMIC_PLATFORM_IOS)
  519. bkpath = filesystem->GetUserDocumentsDir();
  520. #else
  521. bkpath = filesystem->GetAppPreferencesDir("AtomicGameEngine", "Bookmarks");
  522. #endif
  523. bkpath += "/Bookmarks.txt";
  524. String bkdata = "";
  525. int nn=0, sep=-1;
  526. for ( nn = 0; nn<bookmarks_.Size(); nn++)
  527. {
  528. if ( sep > 0 )
  529. {
  530. bkdata += bookmarks_[nn];
  531. bkdata += "\n";
  532. bkdata += bookmarkPaths_[nn];
  533. bkdata += "\n";
  534. }
  535. if ( bookmarks_[nn] == "-" )
  536. sep = nn;
  537. }
  538. File *fp = new File (context_, bkpath, FILE_WRITE);
  539. if(fp->IsOpen())
  540. {
  541. fp->Write ((const void *)bkdata.CString(), bkdata.Length() );
  542. fp->Close();
  543. }
  544. }
  545. }
  546. }