Source Code (Use browser search to find items of interest.)

Class Index

kdelibs'KFileBookmarkManager (./kdelibs/kfile/kfilebookmark.h:64)

class KFileBookmarkManager : public QObject
{
	Q_OBJECT
public:
	KFileBookmarkManager();

	void setTitle( const QString& t )
		{	title = t; }

	void read( const QString& filename );
	void write( const QString& filename );

	void add( const QString& _text, const QString& _url );
	// rich
	bool remove(int);
	bool moveUp(int);
	bool moveDown(int);
	void reread();
	void rename(int, const QString&);
	/**
	 * Overloaded to reread the last file loaded
	 */
	void write();

	KFileBookmark *getBookmark( int id );
	KFileBookmark *getRoot()	{	return &root; }

private:
	const char *parse( BookmarkTokenizer *ht, KFileBookmark *parent, const char *_end);
	void	writeFolder( QTextStream &stream, KFileBookmark *parent );
	KFileBookmark *findBookmark( KFileBookmark *parent, int id, int &currId );

signals:
	void changed();

private:
	KFileBookmark root;
	QString title;
	// rich
        QString myFilename;
};

kdelibs'KFileBookmarkManager::KFileBookmarkManager() (./kdelibs/kfile/kfilebookmark.cpp:65)

KFileBookmarkManager::KFileBookmarkManager()
{
}


kdelibs'KFileBookmarkManager::read() (./kdelibs/kfile/kfilebookmark.cpp:69)

void KFileBookmarkManager::read( const QString& filename )
{
	QFile file( filename );

	// rich
	myFilename= filename;

	if ( !file.open( IO_ReadOnly ) )
		return;

	root.clear();

	QString text;
	char buffer[256];

	do
	{
		file.readLine( buffer, 256 );
		text += QString::fromLatin1(buffer);
	}
	while ( !file.atEnd() );

	BookmarkTokenizer *ht = new BookmarkTokenizer();

	ht->begin();
	ht->write( text );
	ht->end();

	const char *str;

	while ( ht->hasMoreTokens() )
	{
		str = ht->nextToken();

		// Every tag starts with an escape character
		if ( str[0] == TAG_ESCAPE )
		{
			str++;
	
			if ( strncasecmp( str, "<title>", 7 ) == 0 )
			{
				QString t = QString::fromLatin1("");
				bool bend = false;

				do
				{
					if ( !ht->hasMoreTokens() )
						bend = true;
					else
					{
						str = ht->nextToken();
						if ( str[0] == TAG_ESCAPE &&
								strncasecmp( str + 1, "</title>", 8 ) == 0 )
							bend = true;
						else
							t += QString::fromLatin1(str);
					}
				}
				while ( !bend );

				title = t;
			}
			else if ( strncasecmp( str, "<DL>", 4 ) == 0 )
			{
				parse( ht, &root, "" );
			}
		}
    }

	delete ht;

	emit changed();
}

// parser based on HTML widget parser
//

kdelibs'KFileBookmarkManager::parse() (./kdelibs/kfile/kfilebookmark.cpp:145)

const char *KFileBookmarkManager::parse( BookmarkTokenizer *ht, KFileBookmark *parent,
	const char *_end )
{
	KFileBookmark *bm = parent;
	QString text;
	const char *str;

	parent->setType( KFileBookmark::Folder );

	while ( ht->hasMoreTokens() )
	{
		str = ht->nextToken();

		if (str[0] == TAG_ESCAPE )
		{
			str++;

			if ( _end[0] != 0 && strcasecmp( str, _end ) == 0 )
			{
				return str;
			}
			else if ( strncasecmp( str, "<dl>", 4 ) == 0 )
			{
				parse( ht, bm, "</dl>" );
			}
			else if ( strncasecmp( str, "<dt>", 4 ) == 0 )
			{
				bm = new KFileBookmark;
				parent->getChildren().append( bm );
			}
			else if ( strncasecmp( str, "<a ", 3 ) == 0 )
			{
				const char *p = str + 3;

				while ( *p != '>' )
				{
					if ( strncasecmp( p, "href=", 5 ) == 0 )
					{
						p += 5;

						text = "";
						bool quoted = false;
						while ( ( *p != ' ' && *p != '>' ) || quoted )
						{
							if ( *p == '\"' )
								quoted = !quoted;
							else
								text += *p;
							p++;
						}

						bm->setURL( text );
			
						if ( *p == ' ' )
							p++;
					}
					else
					{
						const char *p2 = strchr( p, ' ' );
						if ( p2 == 0L )
							p2 = strchr( p, '>');
						else
							p2++;
						p = p2;
					}
				}

				text = "";
			}
			else if ( strncasecmp( str, "<H3", 3 ) == 0 )
			{
				text = "";
			}
			else if ( strncasecmp( str, "</H3>", 5 ) == 0 ||
						strncasecmp( str, "</a>", 4 ) == 0 )
			{
				bm->setText( text );
			}
		}
		else if ( str[0] )
		{
			text += QString::fromLatin1(str);
		}
	}

	return NULL;
}

// write bookmarks file
//

kdelibs'KFileBookmarkManager::write() (./kdelibs/kfile/kfilebookmark.cpp:235)

void KFileBookmarkManager::write( const QString& filename )
{
	QFile file( filename );

	if ( !file.open( IO_WriteOnly ) )
		return;

	// rich
	myFilename= filename;

	QTextStream stream( &file );

	stream << "<!DOCTYPE KDEHELP-Bookmark-file>" << endl;
	stream << i18n("<!-- Do not edit this file -->") << endl;
	stream << "<TITLE>" << title << "</TITLE>" << endl;

	stream << "<H1>" << title << "</H1>" << endl;

	stream << "<DL><p>" << endl;

	writeFolder( stream, &root );

	stream << "</DL><P>" << endl;
}

// write the contents of a folder (recursive)
//

kdelibs'KFileBookmarkManager::writeFolder() (./kdelibs/kfile/kfilebookmark.cpp:262)

void KFileBookmarkManager::writeFolder( QTextStream &stream, KFileBookmark *parent )
{
	KFileBookmark *bm;

	for ( bm = parent->getChildren().first(); bm != NULL;
			bm = parent->getChildren().next() )
	{
		if ( bm->getType() == KFileBookmark::URL )
		{
			stream << "<DT><A HREF=\"" << bm->getURL() << "\">"
				<< bm->getText() << "</A>" << endl;
		}
		else
		{
			stream << "<DT><H3>" << bm->getText() << "</H3>" << endl;
			stream << "<DL><P>" << endl;
			writeFolder( stream, bm );
			stream << "</DL><P>" << endl;
		}
	}
}


kdelibs'KFileBookmarkManager::getBookmark() (./kdelibs/kfile/kfilebookmark.cpp:284)

KFileBookmark *KFileBookmarkManager::getBookmark( int id )
{
	int currId = 0;

	return findBookmark( &root, id, currId );
}


kdelibs'KFileBookmarkManager::findBookmark() (./kdelibs/kfile/kfilebookmark.cpp:291)

KFileBookmark *KFileBookmarkManager::findBookmark( KFileBookmark *parent, int id,
	int &currId )
{
	KFileBookmark *bm;

	for ( bm = parent->getChildren().first(); bm != NULL;
			bm = parent->getChildren().next() )
	{
		if ( bm->getType() == KFileBookmark::URL )
		{
			if ( currId == id )
				return bm;
			currId++;
		}
		else
		{
			KFileBookmark *retbm;
			if ( ( retbm = findBookmark( bm, id, currId ) ) != NULL )
				return retbm;
		}
	}

	return NULL;
}


kdelibs'KFileBookmarkManager::add() (./kdelibs/kfile/kfilebookmark.cpp:316)

void KFileBookmarkManager::add( const QString& _text, const QString& _url )
{
	root.getChildren().append( new KFileBookmark( _text, _url ) );

	emit changed();
}

// rich

kdelibs'KFileBookmarkManager::remove() (./kdelibs/kfile/kfilebookmark.cpp:324)

bool KFileBookmarkManager::remove(int i)
{
  bool result= false;
  if (i >= 0) {
    root.getChildren().remove(i);
    emit changed();
    result= true;
  }
  return result;
}

// rich

kdelibs'KFileBookmarkManager::rename() (./kdelibs/kfile/kfilebookmark.cpp:336)

void KFileBookmarkManager::rename(int i, const QString& s)
{
  KFileBookmark *b;

  if (i > 0) {
    b= root.getChildren().at(i);
    b->setText(s);
    emit changed();
  }
}

// rich

kdelibs'KFileBookmarkManager::moveUp() (./kdelibs/kfile/kfilebookmark.cpp:348)

bool KFileBookmarkManager::moveUp(int i)
{
  KFileBookmark *b;
  bool result= false;

  if (i > 0) {
    b= root.getChildren().take(i);
    root.getChildren().insert(i-1, b);
    emit changed();
    result= true;
  }
  return result;
}

// rich

kdelibs'KFileBookmarkManager::moveDown() (./kdelibs/kfile/kfilebookmark.cpp:363)

bool KFileBookmarkManager::moveDown(int i)
{
  KFileBookmark *b;
  uint j= i;
  bool result= false;

  if (j < (root.getChildren().count() -1)) {
    b= root.getChildren().take(i);
    root.getChildren().insert(i+1, b);
    emit changed();
    result= true;
  }
  return result;
}

// rich

kdelibs'KFileBookmarkManager::reread() (./kdelibs/kfile/kfilebookmark.cpp:379)

void KFileBookmarkManager::reread()
{
  read(myFilename);
}

// rich

kdelibs'KFileBookmarkManager::write() (./kdelibs/kfile/kfilebookmark.cpp:385)

void KFileBookmarkManager::write()
{
  write(myFilename);
}