//--------------------------------------------------------------------------- #include #pragma hdrstop #include "BookMarkPanel.h" #pragma package(smart_init) #include "newbmfilefrm.h" //#include #include #include #include //--------------------------------------------------------------------------- // ValidCtrCheck is used to assure that the components created do not have // any pure virtual functions. // static inline void ValidCtrCheck(TBookMarkPanel *) { new TBookMarkPanel(NULL); } //--------------------------------------------------------------------------- __fastcall TBookMarkPanel::TBookMarkPanel(TComponent* Owner) : TPanel(Owner) { BMPopup = new TPopupMenu(this); TMenuItem * newitem; newitem = new TMenuItem(BMPopup); newitem->Caption = "&Add Child"; newitem->OnClick = AddChild1Click; BMPopup->Items->Add(newitem); newitem = new TMenuItem(BMPopup); newitem->Caption = "&Delete"; newitem->OnClick = Delete1Click; BMPopup->Items->Add(newitem); newitem = new TMenuItem(BMPopup); newitem->Caption = "&Rename"; newitem->OnClick = Rename1Click; BMPopup->Items->Add(newitem); newitem = new TMenuItem(BMPopup); newitem->Caption = "-"; BMPopup->Items->Add(newitem); newitem = new TMenuItem(BMPopup); newitem->Caption = "&New Bookmark File"; newitem->OnClick = NewBookmarkFile1Click; BMPopup->Items->Add(newitem); } __fastcall TBookMarkPanel::~TBookMarkPanel() { list ::iterator it; SaveBookmarks(); for (it = bmfiles.begin(); it != bmfiles.end(); it++) delete *it; } void __fastcall TBookMarkPanel::CreateWnd() { TPanel::CreateWnd(); SWConfig *bookmarks; SectionMap::iterator sit; ConfigEntMap::iterator eit; TTreeNode *node; DIR *dir; struct dirent *ent; string conffile; bmdir = ""; SWConfig optionsconf("./options.conf"); this->DragMode = dmAutomatic; this->DragKind = dkDock; this->UseDockManager = true; bmtree = new TTreeView(this); bmtree->Parent = this; bmtree->Align = alClient; bmtree->Items->Clear(); bmtree->PopupMenu = BMPopup; if ((sit = optionsconf.Sections.find("Bookmarks")) != optionsconf.Sections.end()) bmdir = ((eit = (*sit).second.find("Directory")) != (*sit).second.end()) ? (*eit).second : (string)""; // Add Personal Bookmarks first, or if they don't exist, ADD A BLANK BRANCH first in the tree // -------------------------------------------------------------------------- if (bmdir == "") bmdir = "./bookmarks/"; if (access(bmdir.c_str(), 0)) { // directory does not exist _mkdir(bmdir.c_str()); } conffile = bmdir + "personal.conf"; bookmarks = new SWConfig(conffile.c_str()); if ((sit = bookmarks->Sections.find("ROOT")) != bookmarks->Sections.end()) { if ((eit = (*sit).second.begin()) != (*sit).second.end()) { node = bmtree->Items->AddObject(bmtree->Selected, (*eit).second.c_str(), *bmfiles.insert(bmfiles.begin(), new String(conffile.c_str()))); AddSection(bookmarks, bmtree, node, (*eit).first.c_str()); } } else bmtree->Items->AddObject(bmtree->Selected, "Personal Bookmarks", *bmfiles.insert(bmfiles.begin(), new String(conffile.c_str()))); delete bookmarks; // -------------------------------------------------------------------------- // Add all other bookmark files --------------------------------------------- if (dir = opendir(bmdir.c_str())) { rewinddir(dir); while ((ent = readdir(dir))) { if ((strcmp(ent->d_name, "personal.conf")) && (strcmp(ent->d_name, "."))&& (strcmp(ent->d_name, ".."))) { conffile = bmdir; conffile += ent->d_name; bookmarks = new SWConfig(conffile.c_str()); if ((sit = bookmarks->Sections.find("ROOT")) != bookmarks->Sections.end()) { if ((eit = (*sit).second.begin()) != (*sit).second.end()) { // Currently supports only ONE topsection per file because on save, each topsection designates which file to rewrite node = bmtree->Items->AddObject(bmtree->Selected, (*eit).second.c_str(), *bmfiles.insert(bmfiles.begin(), new String(conffile.c_str()))); AddSection(bookmarks, bmtree, node, (*eit).first.c_str()); } } delete bookmarks; } } closedir(dir); } /* SWConfig *bookmarks; SectionMap::iterator sit; ConfigEntMap::iterator eit; TTreeNode *node; DIR *dir; struct dirent *ent; string conffile; bmdir = ""; SWConfig optionsconf("./options.conf"); this->DragMode = dmAutomatic; this->DragKind = dkDock; this->UseDockManager = true; bmtree = new TTreeView(this); bmtree->Parent = this; bmtree->Align = alClient; bmtree->DragMode = dmAutomatic; bmtree->Indent = 19; bmtree->PopupMenu = BMPopup; bmtree->TabOrder = 0; bmtree->OnDblClick = bmtreeDblClick; bmtree->OnDragDrop = bmtreeDragDrop; bmtree->OnDragOver = bmtreeDragOver; bmtree->Items->Clear(); if ((sit = optionsconf.Sections.find("Bookmarks")) != optionsconf.Sections.end()) bmdir = (*sit).second["Directory"]; // Add Personal Bookmarks first, or if they don't exist, ADD A BLANK BRANCH first in the tree // -------------------------------------------------------------------------- if (bmdir == "") bmdir = "./bookmarks/"; if (access(bmdir.c_str(), 0)) { // directory does not exist _mkdir(bmdir.c_str()); } conffile = bmdir + "personal.conf"; bookmarks = new SWConfig(conffile); if ((sit = bookmarks->Sections.find("ROOT")) != bookmarks->Sections.end()) { if ((eit = (*sit).second.begin()) != (*sit).second.end()) { node = bmtree->Items->AddObject(bmtree->Selected, (*eit).second.c_str(), *bmfiles.insert(bmfiles.begin(), new String(conffile.c_str()))); AddSection(bookmarks, bmtree, node, (*eit).first.c_str()); } } else bmtree->Items->AddObject(bmtree->Selected, "Personal Bookmarks", *bmfiles.insert(bmfiles.begin(), new String(conffile.c_str()))); delete bookmarks; // -------------------------------------------------------------------------- // Add all other bookmark files --------------------------------------------- if (dir = opendir(bmdir.c_str())) { rewinddir(dir); while ((ent = readdir(dir))) { if ((strcmp(ent->d_name, "personal.conf")) && (strcmp(ent->d_name, "."))&& (strcmp(ent->d_name, ".."))) { conffile = bmdir; conffile += ent->d_name; bookmarks = new SWConfig(conffile); if ((sit = bookmarks->Sections.find("ROOT")) != bookmarks->Sections.end()) { if ((eit = (*sit).second.begin()) != (*sit).second.end()) { // Currently supports only ONE topsection per file because on save, each topsection designates which file to rewrite node = bmtree->Items->AddObject(bmtree->Selected, (*eit).second.c_str(), *bmfiles.insert(bmfiles.begin(), new String(conffile.c_str()))); AddSection(bookmarks, bmtree, node, (*eit).first.c_str()); } } delete bookmarks; } } closedir(dir); } */ } void TBookMarkPanel::AddSection(SWConfig *config, TTreeView *tree, TTreeNode *parent, String section) { SectionMap::iterator sit; ConfigEntMap::iterator eit; TTreeNode *node; if ((sit = config->Sections.find(section.c_str())) != config->Sections.end()) { for (eit = (*sit).second.begin(); eit != (*sit).second.end(); eit++) { node = tree->Items->AddChild(parent, (*eit).second.c_str()); AddSection(config, tree, node, (*eit).first.c_str()); } } } void __fastcall TBookMarkPanel::bmtreeDragDrop(TObject *Sender, TObject *Source, int X, int Y) { bmtree->Selected->MoveTo(bmtree->DropTarget, naAddChildFirst); } //--------------------------------------------------------------------------- void __fastcall TBookMarkPanel::bmtreeDragOver(TObject *Sender, TObject *Source, int X, int Y, TDragState State, bool &Accept) { Accept = false; if (String(Source->ClassName()) == "TTreeView") { if (Source == bmtree) { if (bmtree->Selected->Data) { if (strcmp((*(String*)(bmtree->Selected->Data)).c_str(),(bmdir + "personal.conf").c_str())) { Accept = true; } } else Accept = true; } } } //--------------------------------------------------------------------------- void __fastcall TBookMarkPanel::bmtreeDblClick(TObject *Sender) { if (!bmtree->Selected->getFirstChild()) { // Form1->DefaultVSKey = bmtree->Selected->Text.c_str(); // Form1->TextKeyChanged(); } } //--------------------------------------------------------------------------- void __fastcall TBookMarkPanel::AddChild1Click(TObject *Sender) { bmtree->Selected->Expand(false); bmtree->Items->AddChildFirst(bmtree->Selected, "New Topic")->EditText(); } //--------------------------------------------------------------------------- void __fastcall TBookMarkPanel::Delete1Click(TObject *Sender) { if (bmtree->Selected->Data) { if (strcmp((*(String*)(bmtree->Selected->Data)).c_str(),(bmdir + "personal.conf").c_str())) { bmtree->Selected->Delete(); } } else bmtree->Selected->Delete(); } //--------------------------------------------------------------------------- void __fastcall TBookMarkPanel::Rename1Click(TObject *Sender) { bmtree->Selected->EditText(); } //--------------------------------------------------------------------------- void TBookMarkPanel::SaveBookmarks() { TTreeNode *tree = 0; SWConfig *bmconf; ConfigEntMap emap; SectionMap::iterator sit; char buf[15]; bool personal, other; list ::iterator it; string persfile; SWConfig optionsconf("./options.conf"); if (bmtree->Items->Count) tree = bmtree->Items->Item[0]; if ((sit = optionsconf.Sections.find("Bookmarks")) != optionsconf.Sections.end()) { personal = (atoi((*(*sit).second.find("AutoSavePersonal")).second.c_str())) ? true:false; other = (atoi((*(*sit).second.find("AutoSaveOther")).second.c_str())) ? true:false; } persfile = bmdir + "personal.conf"; for (it = bmfiles.begin(); it != bmfiles.end(); it++) { // delete all bookmark files before saving in case a top level was deleted if (((!strcmp((*it)->c_str(), persfile.c_str())) && personal) || ((strcmp((*it)->c_str(), persfile.c_str())) && other)) unlink((*it)->c_str()); } for (;tree;tree = tree->getNextSibling()) { if (((*((String *)tree->Data) == persfile.c_str()) && personal) || ((*((String *)tree->Data) != persfile.c_str()) && other)) { bmconf = new SWConfig(((String *)tree->Data)->c_str()); emap = bmconf->Sections["ROOT"]; sprintf(buf, "branch%d", tree->AbsoluteIndex); emap.erase(buf); emap.insert(ConfigEntMap::value_type(buf, tree->Text.c_str())); AddSectionToConf(bmconf, buf, tree); bmconf->Sections["ROOT"] = emap; bmconf->Save(); delete bmconf; } } } void TBookMarkPanel::AddSectionToConf(SWConfig *config, String section, TTreeNode *tree) { ConfigEntMap sit; char buf[15]; if (tree = tree->getFirstChild()) { sit = config->Sections[section.c_str()]; for (; tree; tree = tree->getNextSibling()) { sprintf(buf, "branch%d", tree->AbsoluteIndex); sit.erase(buf); sit.insert(ConfigEntMap::value_type(buf, tree->Text.c_str())); AddSectionToConf(config, buf, tree); } config->Sections[section.c_str()] = sit; } } void __fastcall TBookMarkPanel::NewBookmarkFile1Click(TObject *Sender) { if (NewBMfrm->ShowModal() == mrOk) { bmtree->Items->AddObject(bmtree->Items->Item[0], NewBMfrm->bmtitle->Text, *bmfiles.insert(bmfiles.begin(), new String(String(bmdir.c_str()) + NewBMfrm->bmfile->Text + String(".conf")))); } } //--------------------------------------------------------------------------- namespace Bookmarkpanel { void __fastcall PACKAGE Register() { TComponentClass classes[1] = {__classid(TBookMarkPanel)}; RegisterComponents("Samples", classes, 0); } } //---------------------------------------------------------------------------