aboutsummaryrefslogtreecommitdiffstats
path: root/apps/windoze/CBuilder5/BibleCS/bookmarkfrm.cpp
blob: 4429c5d0f2ed9cb06f46c1dbd553747258c5ff12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "bookmarkfrm.h"
#include <swconfig.h>
#include <dirent.h>
#include "mainfrm.h"
#include "newbmfilefrm.h"
#include <io.h>
#include <dir.h>

//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TbookmarkForm *bookmarkForm;
//---------------------------------------------------------------------------
__fastcall TbookmarkForm::TbookmarkForm(TComponent* Owner)
	: TForm(Owner)
{
	SWConfig *bookmarks;
	SectionMap::iterator sit;
	ConfigEntMap::iterator eit;
	TTreeNode *node;
	
	DIR *dir;
	struct dirent *ent;
	string conffile;
	bmdir = "";
	
	bmtree->Items->Clear();

	if ((sit = Form1->optionsconf->Sections.find("Bookmarks")) != Form1->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);
	}
}


__fastcall TbookmarkForm::~TbookmarkForm() {
	list <String *>::iterator it;

	SaveBookmarks();

	for (it = bmfiles.begin(); it != bmfiles.end(); it++)
		delete *it;
}


//--------------------------------------------------------------------------- 

void TbookmarkForm::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 TbookmarkForm::bmtreeDragDrop(TObject *Sender, TObject *Source,
	int X, int Y)
{
	bmtree->Selected->MoveTo(bmtree->DropTarget, naAddChildFirst);
}
//---------------------------------------------------------------------------
void __fastcall TbookmarkForm::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 TbookmarkForm::bmtreeDblClick(TObject *Sender)
{
	if (!bmtree->Selected->getFirstChild()) {
		*(Form1->DefaultVSKey) = bmtree->Selected->Text.c_str();	
		Form1->TextKeyChanged();
	}
	
}
//---------------------------------------------------------------------------
void __fastcall TbookmarkForm::AddChild1Click(TObject *Sender)
{
	bmtree->Selected->Expand(false);
	bmtree->Items->AddChildFirst(bmtree->Selected, "New Topic")->EditText();	
}
//---------------------------------------------------------------------------
void __fastcall TbookmarkForm::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 TbookmarkForm::Rename1Click(TObject *Sender)
{
	bmtree->Selected->EditText();	
}
//---------------------------------------------------------------------------

void TbookmarkForm::SaveBookmarks()
{
	TTreeNode *tree = 0;
	SWConfig *bmconf;
	ConfigEntMap emap;
	SectionMap::iterator sit;
	char buf[15];
	bool personal, other;
	list <String *>::iterator it;
	string persfile;

	if (bmtree->Items->Count)
		tree = bmtree->Items->Item[0];

	if ((sit = Form1->optionsconf->Sections.find("Bookmarks")) != Form1->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 TbookmarkForm::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 TbookmarkForm::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"))));
	}
}
//---------------------------------------------------------------------------