⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 krbookmarkhandler.cpp

📁 LINUX 下, 以 QT/KDE 写的档案管理员
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "krbookmarkhandler.h"#include "kraddbookmarkdlg.h"#include "../krusader.h"#include "../krslots.h"#include "../Dialogs/popularurls.h"#include "../VFS/vfs.h"#include <kiconloader.h>#include <kmessagebox.h>#include <qptrlist.h>#include <kactioncollection.h>#include <klocale.h>#include <kdebug.h>#include <kbookmarkmanager.h>#include <kstandarddirs.h>#include <qfile.h>#include <qcursor.h>#define SPECIAL_BOOKMARKS	true// ------------------------ for internal use#define BOOKMARKS_FILE	"krusader/krbookmarks.xml"#define CONNECT_BM(X)	{ disconnect(X, SIGNAL(activated(const KURL&)), 0, 0); connect(X, SIGNAL(activated(const KURL&)), this, SLOT(slotActivated(const KURL&))); }											KrBookmarkHandler::KrBookmarkHandler(): QObject(0), _middleClick(false), _mainBookmarkPopup( 0 ), _specialBookmarkIDs(), _bookmarkIDTable() {	// create our own action collection and make the shortcuts apply only to parent	_privateCollection = new KActionCollection(krApp, "private collection");	_collection = krApp->actionCollection();	// create _root: father of all bookmarks. it is a dummy bookmark and never shown	_root = new KrBookmark(i18n("Bookmarks"));		_bookmarkIDTable.setAutoDelete( true );		// load bookmarks 	importFromFile();	// hack	manager = KBookmarkManager::managerForFile(locateLocal( "data", BOOKMARKS_FILE ), false);	connect(manager, SIGNAL(changed(const QString&, const QString& )), this, SLOT(bookmarksChanged(const QString&, const QString& )));}KrBookmarkHandler::~KrBookmarkHandler() {	delete manager;	delete _privateCollection;}void KrBookmarkHandler::menuOperation(int id) {	switch (id) {		case BookmarkCurrent:			bookmarkCurrent(ACTIVE_PANEL->virtualPath());			break;		case ManageBookmarks:			manager->slotEditBookmarks();			break;	}}void KrBookmarkHandler::bookmarkCurrent(KURL url) {	KrAddBookmarkDlg dlg(krApp, url);	if (dlg.exec() == KDialog::Accepted) {		KrBookmark *bm = new KrBookmark(dlg.name(), dlg.url(), _collection);		addBookmark(bm, dlg.folder());	}}void KrBookmarkHandler::addBookmark(KrBookmark *bm, KrBookmark *folder) {	if (folder == 0)		folder = _root;			// add to the list (bottom)	folder->children().append(bm);	exportToFile();}void KrBookmarkHandler::deleteBookmark(KrBookmark *bm) {	if( bm->isFolder() )		clearBookmarks( bm ); // remove the child bookmarks	removeReferences( _root, bm );	bm->unplugAll();	delete bm;		exportToFile();}void KrBookmarkHandler::removeReferences( KrBookmark *root, KrBookmark *bmToRemove ) {	int index = root->children().find( bmToRemove );	if( index >= 0 )		root->children().take( index );		KrBookmark *bm = root->children().first();	while (bm) {		if (bm->isFolder())			removeReferences(bm, bmToRemove);		bm = root->children().next();	}}void KrBookmarkHandler::exportToFileBookmark(QDomDocument &doc, QDomElement &where, KrBookmark *bm) {	if( bm->isSeparator() ) {		QDomElement bookmark = doc.createElement("separator");		where.appendChild(bookmark);	}	else {		QDomElement bookmark = doc.createElement("bookmark");		// url		bookmark.setAttribute("href", bm->url().prettyURL());		// icon		bookmark.setAttribute("icon", bm->icon());		// title		QDomElement title = doc.createElement("title");			title.appendChild(doc.createTextNode(bm->text()));		bookmark.appendChild(title);				where.appendChild(bookmark);	}}void KrBookmarkHandler::exportToFileFolder(QDomDocument &doc, QDomElement &parent, KrBookmark *folder) {	for (KrBookmark *bm = folder->children().first(); bm; bm = folder->children().next()) {		if (bm->isFolder()) {			QDomElement newFolder = doc.createElement("folder");			newFolder.setAttribute("icon", bm->icon());			parent.appendChild(newFolder);			QDomElement title = doc.createElement("title");			title.appendChild(doc.createTextNode(bm->text()));			newFolder.appendChild(title);			exportToFileFolder(doc, newFolder, bm);		} else {			exportToFileBookmark(doc, parent, bm);		}	}}// export to file using the xbel standard////  <xbel>//    <bookmark href="http://developer.kde.org"><title>Developer Web Site</title></bookmark>//    <folder folded="no">//      <title>Title of this folder</title>//      <bookmark icon="kde" href="http://www.kde.org"><title>KDE Web Site</title></bookmark>//      <folder toolbar="yes">//        <title>My own bookmarks</title>//        <bookmark href="http://www.koffice.org"><title>KOffice Web Site</title></bookmark>//        <separator/>//        <bookmark href="http://www.kdevelop.org"><title>KDevelop Web Site</title></bookmark>//      </folder>//    </folder>//  </xbel>void KrBookmarkHandler::exportToFile() {	QDomDocument doc( "xbel" );   QDomElement root = doc.createElement( "xbel" );   doc.appendChild( root );	exportToFileFolder(doc, root, _root);	if (!doc.firstChild().isProcessingInstruction()) {		// adding: <?xml version="1.0" encoding="UTF-8" ?> if not already present 		QDomProcessingInstruction instr = doc.createProcessingInstruction( "xml", 				"version=\"1.0\" encoding=\"UTF-8\" ");		doc.insertBefore( instr, doc.firstChild() ); 	}		QString filename = locateLocal( "data", BOOKMARKS_FILE );	QFile file(filename);	if ( file.open( IO_WriteOnly ) ) {		QTextStream stream( &file );		stream.setEncoding(stream.UnicodeUTF8);		stream << doc.toString();		file.close();	} else {		KMessageBox::error(krApp, i18n("Unable to write to %1").arg(filename), i18n("Error"));	}}bool KrBookmarkHandler::importFromFileBookmark(QDomElement &e, KrBookmark *parent, QString path, QString *errorMsg) {	QString url, name, icon;	// verify tag	if (e.tagName() != "bookmark") {		*errorMsg = e.tagName() + i18n(" instead of ")+"bookmark";		return false;	}	// verify href	if (!e.hasAttribute("href")) {		*errorMsg = i18n("missing tag ")+ "href";		return false;	} else url = e.attribute("href");	// verify title	QDomElement te = e.firstChild().toElement();	if (te.tagName() != "title") {		*errorMsg = i18n("missing tag ")+"title";		return false;	} else name = te.text();	// do we have an icon?	if (e.hasAttribute("icon")) {		icon=e.attribute("icon");	}	// ok: got name and url, let's add a bookmark	KrBookmark *bm = KrBookmark::getExistingBookmark(path+name, _collection);	if (!bm) {		bm = new KrBookmark(name, vfs::fromPathOrURL( url ), _collection, icon, path+name);	parent->children().append(bm);	}	return true;}bool KrBookmarkHandler::importFromFileFolder(QDomNode &first, KrBookmark *parent, QString path, QString *errorMsg) {	QString name;	QDomNode n = first;	while (!n.isNull()) {		QDomElement e = n.toElement();		if (e.tagName() == "bookmark") {			if (!importFromFileBookmark(e, parent, path, errorMsg))				return false;		} else if (e.tagName() == "folder") {			QString iconName = "";			if (e.hasAttribute("icon")) iconName=e.attribute("icon");			// the title is the first child of the folder			QDomElement tmp = e.firstChild().toElement();			if (tmp.tagName() != "title") {				*errorMsg = i18n("missing tag ")+"title";				return false;			} else name = tmp.text();			KrBookmark *folder = new KrBookmark(name, iconName);			parent->children().append(folder);			QDomNode nextOne = tmp.nextSibling();			if (!importFromFileFolder(nextOne, folder, path + name + "/", errorMsg))				return false;		} else if (e.tagName() == "separator") {			parent->children().append(KrBookmark::separator());		}		n = n.nextSibling();	}	return true;}void KrBookmarkHandler::importFromFile() {	clearBookmarks(_root);		QString filename = locateLocal( "data", BOOKMARKS_FILE );	QFile file( filename );	if ( !file.open(IO_ReadOnly))		return; // no bookmarks file	QString errorMsg;	QDomNode n;	QDomElement e;	QDomDocument doc( "xbel" );	if ( !doc.setContent( &file, &errorMsg ) ) {		goto ERROR;	}	// iterate through the document: first child should be "xbel" (skip all until we find it)	n = doc.firstChild();		while (!n.isNull() && n.toElement().tagName()!="xbel")		n = n.nextSibling();	if (n.isNull() || n.toElement().tagName()!="xbel") {		errorMsg = i18n("%1 doesn't seem to be a valid Bookmarks file").arg(filename);		goto ERROR;	} else n = n.firstChild(); // skip the xbel part	importFromFileFolder(n, _root, "", &errorMsg);	goto SUCCESS;	ERROR:	KMessageBox::error(krApp, i18n("Error reading bookmarks file: %1").arg(errorMsg), i18n( "Error" ));SUCCESS:	file.close();}void KrBookmarkHandler::populate(KPopupMenu *menu) {	_mainBookmarkPopup = menu;	menu->clear();	_bookmarkIDTable.clear();	_specialBookmarkIDs.clear();	buildMenu(_root, menu);}void KrBookmarkHandler::buildMenu(KrBookmark *parent, KPopupMenu *menu) {	static int inSecondaryMenu = 0; // used to know if we're on the top menu	// run the loop twice, in order to put the folders on top. stupid but easy :-)	// note: this code drops the separators put there by the user	for (KrBookmark *bm = parent->children().first(); bm; bm = parent->children().next()) {		if (!bm->isFolder()) continue;		KPopupMenu *newMenu = new KPopupMenu(menu);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -