1010#include < QFile>
1111#include < QTextStream>
1212#include < QLoggingCategory>
13+ #include < QJsonArray>
1314
1415#include " BookmarkDatabase.h"
1516
@@ -112,7 +113,7 @@ bool CBookmarkDatabase::moveFolder(int folderId, int newParentId)
112113
113114BookmarkItem CBookmarkDatabase::getBookmark (int id)
114115{
115- BookmarkItem item;
116+ BookmarkItem item (BookmarkType_Bookmark) ;
116117
117118 auto leaf = m_TreeDB.GetLeaf (id);
118119 if (leaf.GetId () == 0 ) return item;
@@ -123,7 +124,7 @@ BookmarkItem CBookmarkDatabase::getBookmark(int id)
123124QList<BookmarkItem> CBookmarkDatabase::getBookmarkByUrl (const QString &url)
124125{
125126 QList<BookmarkItem> lstItems;
126- BookmarkItem item;
127+ BookmarkItem item (BookmarkType_Bookmark) ;
127128
128129 auto urlItem = m_UrlDB.GetItem (url);
129130 auto leaves = m_TreeDB.GetLeavesByKey (urlItem.id );
@@ -532,7 +533,7 @@ int CBookmarkDatabase::importBookmark(const QDomElement &aElement,
532533 QDateTime lastModified = parseTimestamp (lastModifiedStr);
533534
534535 // 创建书签对象
535- BookmarkItem item;
536+ BookmarkItem item (BookmarkType_Bookmark) ;
536537 item.url = url;
537538 item.title = title;
538539 item.createdTime = addDate.isValid () ? addDate : QDateTime::currentDateTime ();
@@ -721,7 +722,7 @@ TreeItem CBookmarkDatabase::BookmarkToTree(const BookmarkItem& tree, bool setKey
721722
722723BookmarkItem CBookmarkDatabase::TreeToBookmark (const TreeItem& tree)
723724{
724- BookmarkItem item;
725+ BookmarkItem item (BookmarkType_Bookmark) ;
725726 CDatabaseUrl::UrlItem url = m_UrlDB.GetItem (tree.GetKey ());
726727 item = TreeToBookmark (tree, url);
727728 return item;
@@ -730,7 +731,7 @@ BookmarkItem CBookmarkDatabase::TreeToBookmark(const TreeItem& tree)
730731BookmarkItem CBookmarkDatabase::TreeToBookmark (
731732 const TreeItem& tree, const CDatabaseUrl::UrlItem& url)
732733{
733- BookmarkItem item;
734+ BookmarkItem item (BookmarkType_Bookmark) ;
734735 item.id = tree.GetId ();
735736 item.folderId = tree.GetParentId ();
736737 item.createdTime = tree.GetCreateTime ();
@@ -749,13 +750,96 @@ BookmarkItem CBookmarkDatabase::TreeToBookmark(
749750 return item;
750751}
751752
752-
753753bool CBookmarkDatabase::ExportToJson (QJsonObject &obj)
754754{
755- return true ;
755+ bool bRet = true ;
756+ QJsonArray root;
757+ bRet = ExportToJson (0 , root);
758+ if (bRet)
759+ obj.insert (" browser_bookmark" , root);
760+ return bRet;
756761}
757762
758763bool CBookmarkDatabase::ImportFromJson (const QJsonObject &obj)
759764{
765+ bool bRet = false ;
766+ QJsonArray bookmark = obj[" browser_bookmark" ].toArray ();
767+ if (bookmark.isEmpty ()) {
768+ SetError (tr (" The file format is error. Json without \" browser_bookmark\" " ));
769+ qCritical (log) << GetError ();
770+ return false ;
771+ }
772+
773+ bRet = ImportFromJson (0 , bookmark);
774+ return bRet;
775+ }
776+
777+ bool CBookmarkDatabase::ImportFromJson (int parentId, const QJsonArray &obj)
778+ {
779+ for (auto it = obj.begin (); it != obj.end (); it++) {
780+ QJsonObject itemObj = it->toObject ();
781+ if (itemObj.isEmpty ()) continue ;
782+ if (BookmarkType::BookmarkType_Folder == itemObj[" type" ].toInt ()) {
783+ QString szName = itemObj[" title" ].toString ();
784+ int id = addFolder (szName, parentId);
785+ QJsonArray items = itemObj[" title" ].toArray ();
786+ if (items.isEmpty ()) continue ;
787+ ImportFromJson (id, items);
788+ continue ;
789+ }
790+
791+ BookmarkItem item (BookmarkType::BookmarkType_Bookmark);
792+ item.title = itemObj[" title" ].toString ();
793+ item.url = itemObj[" url" ].toString ();
794+ item.folderId = parentId;
795+
796+ QIcon icon;
797+ bool bRet = CDatabaseIcon::ImportIconFromJson (itemObj, icon);
798+ if (!bRet) continue ;
799+ item.icon =icon;
800+
801+ addBookmark (item);
802+ }
803+
804+ return true ;
805+ }
806+
807+ bool CBookmarkDatabase::ExportToJson (int parentId, QJsonArray &obj)
808+ {
809+ auto items = getSubFolders (parentId);
810+ foreach (auto item, items) {
811+ QJsonObject oItem;
812+ if (item.isFolder ()) {
813+ QJsonArray aItem;
814+ bool bRet = ExportToJson (item.id , aItem);
815+ if (!bRet) continue ;
816+
817+ oItem.insert (" type" , item.type );
818+ oItem.insert (" title" , item.title );
819+ oItem.insert (item.title , aItem);
820+ } else {
821+ Q_ASSERT_X (false , " ExportBookmark" , " The item is not node" );
822+ continue ;
823+ }
824+
825+ obj.append (oItem);
826+ }
827+
828+ items = getAllBookmarks (parentId);
829+ foreach (auto item, items) {
830+ QJsonObject oItem;
831+ if (item.isBookmark ()) {
832+ oItem.insert (" type" , item.type );
833+ oItem.insert (" title" , item.title );
834+ oItem.insert (" url" , item.url );
835+ // Icon
836+ bool bRet = CDatabaseIcon::ExportIconToJson (item.getIcon (), oItem);
837+ if (!bRet) continue ;
838+ } else {
839+ Q_ASSERT_X (false , " ExportBookmark" , " The item is not bookmark" );
840+ continue ;
841+ }
842+ obj.append (oItem);
843+ }
760844 return true ;
761845}
0 commit comments