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

📄 bookmark.cpp

📁 Last change: 2008-02-03 This is the source code of KCeasy。
💻 CPP
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Bookmark.h"
#include "Config.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

// comparison functor for TBookmark*
class TBookmarkLessThan : public std::binary_function<TBookmark const*,TBookmark const*,bool>
{
public:
   bool operator()(TBookmark const* a, TBookmark const* b) const
   {
        return a->Pos < b->Pos;
   }
};

// class TBookmark

TBookmark::TBookmark(TBookmarkManager* NParent, const string& NUrl, const string& NTitle, int NPos)
:   Parent(NParent),
    Url(NUrl),
    Title(NTitle),
    Pos(NPos)
{
}

TBookmark::~TBookmark()
{
}

// class TBookmarkManager

TBookmarkManager::TBookmarkManager()
{
}

TBookmarkManager::~TBookmarkManager()
{
    Clear();
}

void TBookmarkManager::Clear()
{
    while(!Bookmarks.empty()) {
        delete Bookmarks.front();
        Bookmarks.pop_front();
    }
}

bool TBookmarkManager::Load(const string& File, const char* Section)
{
    Clear();

    TFileConfig* Conf = new TFileConfig(File);
    if(!Conf->Load(true)) {
        delete Conf;
        return false;
    }

    list<string> Names = Conf->GetNames(Section);
    string Sec = string(Section) + "/";

    for(list<string>::iterator nitr = Names.begin(); nitr != Names.end(); ++nitr) {
        list<string> Tokens = string_split(Conf->GetValue(Sec + (*nitr)),"|");
        if(Tokens.size() != 2)
            continue;
        TBookmark* Bookmark = new TBookmark(this,Tokens.front(),Tokens.back(),string_to_int(*nitr));
        Bookmarks.push_back(Bookmark);
    }

    Bookmarks.sort(TBookmarkLessThan());
    RecalcPositions();

    delete Conf;
    return true;
}

bool TBookmarkManager::Save(const string& File, const char* Section)
{
    TFileConfig* Conf = new TFileConfig(File);
    if(!Conf->Load(true)) {
        delete Conf;
        return false;
    }

    Conf->RemoveSection(Section);

    string Sec = string(Section) + "/";

    for(Iterator itr = Begin(); itr != End(); ++itr) {
        string Value = string_replace((*itr)->GetUrl(), "|", "_") + "|";
        Value += string_replace((*itr)->GetTitle(), "|", "_");
        Conf->SetValue(Sec + int_to_string((*itr)->GetPos()), Value);
    }

    if(!Conf->Save()) {
        delete Conf;
        return false;
    }

    delete Conf;
    return true;
}

TBookmark* TBookmarkManager::GetByPos(int Pos)
{
    for(Iterator itr = Begin(); itr != End(); ++itr)
        if((*itr)->GetPos() == Pos)
            return (*itr);
    return NULL;
}

TBookmark* TBookmarkManager::GetByUrl(const string& Url)
{
    for(Iterator itr = Begin(); itr != End(); ++itr)
        if((*itr)->GetUrl() == Url)
            return (*itr);
    return NULL;
}

TBookmark* TBookmarkManager::Add(const string& Url, const string& Title, int Pos)
{
    if(Pos == -1)
        Pos = Bookmarks.size();

    TBookmark* Bookmark = new TBookmark(this,Url,Title,Pos);

    Bookmarks.push_back(Bookmark);
    Bookmarks.sort(TBookmarkLessThan());
    RecalcPositions();

    return Bookmark;
}

bool TBookmarkManager::Delete(TBookmark* Bookmark)
{
    Iterator itr;

    for(itr = Begin(); itr != End() && (*itr) != Bookmark; ++itr);
    if(itr == Bookmarks.end())
        return false;

    Bookmarks.erase(itr);
    RecalcPositions();
    delete Bookmark;

    return true;
}

bool TBookmarkManager::Move(TBookmark* Bookmark, int NewPos)
{
    Iterator itr;

    for(itr = Begin(); itr != End() && (*itr) != Bookmark; ++itr);
    if(itr == Bookmarks.end())
        return false;

    Bookmarks.erase(itr);

    // find new pos

    for(itr = Begin(); itr != End() && (*itr)->GetPos() > NewPos; ++itr);
    Bookmarks.insert(itr,Bookmark);
    RecalcPositions();

    return true;
}

// protected

void TBookmarkManager::RecalcPositions()
{
    // renumber from 0 through Bookmarks.size() - 1
    Iterator itr = Begin();
    for(int i = 0; itr != End(); ++itr, ++i)
        (*itr)->Pos = i;
}

⌨️ 快捷键说明

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