-
Notifications
You must be signed in to change notification settings - Fork 0
/
FNotebook.pas
925 lines (817 loc) · 31.2 KB
/
FNotebook.pas
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
unit FNotebook;
interface
uses
htmlcomp, htmlpars,
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, IOUtils,
htmledit, System.ImageList,
Vcl.ImgList, Vcl.VirtualImageList,
Vcl.BaseImageCollection, Vcl.ImageCollection, System.Actions, Vcl.ActnList,
Vcl.PlatformDefaultStyleActnCtrls, Vcl.ActnMan, Vcl.ExtCtrls, Vcl.ComCtrls,
Vcl.ToolWin, Vcl.ActnCtrls, Vcl.Controls, Vcl.Tabs,
Vcl.Graphics, Vcl.Forms, Vcl.Dialogs,
System.Generics.Collections,
VCL.TMSFNCTypes, JvExComCtrls, JvToolBar, Vcl.StdCtrls, JvExStdCtrls,
JvHtControls, Vcl.FileCtrl, FlCtrlEx,
clRamLog, clRichLog, Vcl.Menus, AdvMemo, advmjson,
Chilkat_v9_5_0_TLB;
type
ZeroBasedInteger = integer;
OneBasedInteger = integer;
type INotebook = interface (IInterface) ['{40BE0C1E-39A5-4728-9CC2-30B84B397234}']
function GetPage(AzbiNotebook: ZeroBasedInteger;
AzbiPage: ZeroBasedInteger = 0): string;
function PutPage(AzbiNotebook: ZeroBasedInteger;
AzbiPage: ZeroBasedInteger;
ABody: string;
AUseEditorFlag: boolean = false): string;
function GetNotebookDir(zbiNotebook: ZeroBasedInteger = -1): string;
function GetPageFilename(zbiPage: ZeroBasedInteger): string;
end;
TNotebook = class(TForm, INotebook)
HtTabSet1: THtTabSet;
HtmlEditor1: THtmlEditor;
tSave: TTimer;
actman: TActionManager;
actPageNext: TAction;
actPagePrev: TAction;
ActionToolBar2: TActionToolBar;
actNotebookNext: TAction;
Action3: TAction;
unvis: TPanel;
imgcol: TImageCollection;
imglst: TVirtualImageList;
StatusBar1: TStatusBar;
actPageExport: TAction;
FileListBoxEx1: TFileListBoxEx;
filePopupMenu: THtPopupMenu;
actPageRename: TAction;
Rename1: TMenuItem;
actPageDelete: TAction;
Delete1: TMenuItem;
lstPages: TListBox;
actGlobalAbout: TAction;
actNotebookRename: TAction;
lstNotebookNames: TListBox;
actPageSave: TAction;
Save1: TMenuItem;
NextTab1: TMenuItem;
N1: TMenuItem;
exportDlg: TSaveDialog;
importDlg: TOpenDialog;
actPageImport: TAction;
ImportPage1: TMenuItem;
N2: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure HtTabSet1Change(Sender: TObject; NewTab: Integer;
var AllowChange: Boolean);
procedure actTabsNextExecute(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure tSaveTimer(Sender: TObject);
procedure actPagePrevExecute(Sender: TObject);
procedure actPageNextExecute(Sender: TObject);
procedure Action3Execute(Sender: TObject);
procedure actNotebookNextExecute(Sender: TObject);
procedure actPageExportExecute(Sender: TObject);
procedure HtmlEditor1UrlClick(Sender: TElement);
procedure FileListBoxEx1Change(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure StatusBar1Click(Sender: TObject);
procedure StatusBar1DblClick(Sender: TObject);
procedure actPageRenameExecute(Sender: TObject);
procedure actPageDeleteExecute(Sender: TObject);
procedure lstPagesClick(Sender: TObject);
procedure lstPagesDblClick(Sender: TObject);
procedure lstPagesKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure lstPagesKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure actGlobalAboutExecute(Sender: TObject);
procedure actNotebookRenameExecute(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure lstPagesMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure actPageSaveExecute(Sender: TObject);
procedure actPageImportExecute(Sender: TObject);
private
{ Private declarations }
metaFile: string;
bDebugMode: boolean;
zbiCurrentNotebook: ZeroBasedInteger;
zbiCurrentPage: ZeroBasedInteger;
AppDataDir: string;
richLog: TRichLog;
ramLog: TRamLog;
loadingMode: string;
notebookNames: TStringList;
procedure Log(message: string; alsoShowMessage: boolean = false);
procedure LoadPage(zbiNewNotebook: ZeroBasedInteger;
zbiNewPage: ZeroBasedInteger = 0);
procedure SavePage;
procedure NextPage;
procedure PrevPage;
procedure ExportPage;
procedure ImportPage;
procedure LaunchBrowser(URL: string);
procedure RefreshPagesListBox(findFilename: string = '<:NONE:>');
procedure EnterLoadingMode(modeName: string);
procedure LeaveLoadingMode(modeName: string);
// function GetJsonMeta(var json: HCkJsonObject; fieldName: string; var success: boolean): string;
// procedure SetJsonMeta(json: HCkJsonObject; fieldName: string; fieldValue: string);
procedure LoadNotebookNames;
public
{ Public declarations }
function GetPage(AzbiNotebook: ZeroBasedInteger;
AzbiPage: ZeroBasedInteger = 0): string;
function PutPage(AzbiNotebook: ZeroBasedInteger;
AzbiPage: ZeroBasedInteger;
ABody: string;
AUseEditorFlag: boolean = false): string;
function GetNotebookDir(zbiNotebook: ZeroBasedInteger = -1): string;
function GetPageFilename(zbiPage: ZeroBasedInteger): string;
end;
var
Notebook: TNotebook;
implementation
{$R *.dfm}
uses ShellApi, System.StrUtils, System.DateUtils, System.JSON, FAbout;
resourcestring
StrUnableToLoadChikc = 'Unable to load chikcat.dll, error recieved was: "%s"';
StrDefaultPage1_Begin = '<html><body><p><b><span style="color: #1F497D">';
StrDefaultPage1_Line2 = '<p>Welcome to the mindfulvector Notebook program!</span></b> <br/><small>aka <b>mvNotebook</b>!</small></p>';
StrDefaultPage1_Line3 = '<p>Each Notebook tab above is a directory under Documents\mvNotebook. '+
'Each one can contain unlimited pages.</p><p>It currently can switch between 10 notebooks, '+
'auto saves every 5 minutes as well as on tab change and on close.</p>';
StrDefaultPage1_Line4 = '<p>Rich text formatting is fairly robust, and '+
'accessed by selecting a piece of text then using the '+
'<span style="background-color: #FFFF00">popup</span>.</p>';
StrDefaultPage1_Line5 = '<p>Additional features will be added soon, such as '+
'file exporting, syncronization, and more.</p><p> </p>';
StrDefaultPage1_Line6 = '<p style="text-align:justify;">Example: '+
'<span style="background-color: #FFFF00">Select this text</span> with your '+
'mouse to see the formatting toolbar</p><p> </p>';
StrDefaultPage1_End = '</body></html>';
const STATUSPANEL_STATE: integer = 0;
const STATUSPANEL_CURRENT_LOCATION: integer = 1;
procedure TNotebook.actPageDeleteExecute(Sender: TObject);
var
currentFilename, newFilename: string;
buttonSelected : Integer;
begin
Log('actPageDeleteExecute ====================================================');
if lstPages.ItemIndex = -1 then
begin
Log('No file selected, ItemIndex is -1');
Exit;
end;
EnterLoadingMode('actPageDelete');
currentFilename := GetPageFilename(lstPages.ItemIndex);
buttonSelected := MessageDlg('Do you want to delete the file: "' + currentFilename + '" from the current notebook?',
mtCustom, [mbYes, mbNo], 0);
if buttonSelected = mrYes then
begin
currentFilename := GetNotebookDir + currentFilename;
if DeleteFile(currentFilename) then
begin
if lstPages.ItemIndex > 0 then
lstPages.ItemIndex := lstPages.ItemIndex - 1
else if lstPages.Count > 0 then
lstPages.ItemIndex := lstPages.ItemIndex + 1
else
lstPages.ItemIndex := -1;
Log(Format('lstPages.ItemIndex( new value )=%d', [lstPages.ItemIndex]));
HtmlEditor1.Visible := false;
Log('File has been deleted!', true);
end else begin
Log('Unable to delete file "'+currentFilename+'"', true);
end;
end;
LeaveLoadingMode('actPageDelete');
if lstPages.ItemIndex > -1 then
LoadPage(zbiCurrentNotebook, lstPages.ItemIndex)
else
LoadPage(zbiCurrentNotebook, 0);
RefreshPagesListBox;
// Mismatch in UI, so just hide the editor until user selects page they
// want
HtmlEditor1.Visible := false;
end;
procedure TNotebook.actPageExportExecute(Sender: TObject);
begin
Log('actPageExportExecute ====================================================');
ExportPage;
end;
procedure TNotebook.actPageNextExecute(Sender: TObject);
begin
Log('actPageNextExecute ======================================================');
NextPage;
end;
procedure TNotebook.actPagePrevExecute(Sender: TObject);
begin
Log('actPagePrevExecute ======================================================');
PrevPage;
end;
procedure TNotebook.actPageRenameExecute(Sender: TObject);
var
currentFilename, newFilenameBasename, newFileFullPath: string;
begin
Log('actPageRenameExecute ====================================================');
currentFilename := GetPageFilename(lstPages.ItemIndex);
newFilenameBasename := InputBox('Rename page', 'Enter new name:', currentFilename);
newFilenameBasename := newFilenameBasename.Trim;
if (newFilenameBasename.Length > 0) and (newFilenameBasename <> currentFilename) then
begin
if not EndsStr('.html', newFilenameBasename) then
newFilenameBasename := newFilenameBasename + '.html';
currentFilename := GetNotebookDir + currentFilename;
newFileFullPath := GetNotebookDir + newFilenameBasename;
Log('Rename file "'+currentFilename+'" to "'+newFileFullPath+'"');
if RenameFile(currentFilename, newFileFullPath) then
begin
Log('File renamed successfully');
RefreshPagesListBox(newFilenameBasename);
end else begin
Log('Unable to rename file "'+currentFilename+'" to "'+newFileFullPath+'"', true);
end;
end;
end;
procedure TNotebook.actPageSaveExecute(Sender: TObject);
begin
SavePage;
end;
procedure TNotebook.actGlobalAboutExecute(Sender: TObject);
begin
AboutBox.ShowModal();
// ShowMessage('soNotebook created by Stone Orb Software. '+#13#10+
// 'Copyright 2023. All rights reserved. '+#13#10+
// #13#10+
// 'The source code for this program is open source, '+#13#10+
// 'but this binary file is not!'+#13#10+
// #13#10+
// 'This program is the work of a single individual, '+#13#10+
// 'mindfulvector, who asks for your help to earn a '+#13#10+
// 'living if you have found this software useful.'+#13#10+
// #13#10+
// 'Please do not share this file, and instead direct '+#13#10+
// 'interested persons to the itch.io site where they '+#13#10+
// 'can purchase their own copy for a very small amount. '+#13#10+
// #13#10+
// 'Thank you for your support!');
end;
procedure TNotebook.actPageImportExecute(Sender: TObject);
begin
if importDlg.Execute then
begin
if not CopyFile(PChar(importDlg.FileName), PChar(GetNotebookDir+'\' + ExtractFileName(importDlg.FileName)), true) then
ShowMessage('File not imported; does it already exist or was the source disk removed?')
else
RefreshPagesListBox;
end;
end;
procedure TNotebook.Action3Execute(Sender: TObject);
begin
Log('Action3Execute ==========================================================');
HtTabSet1.SelectNext(true);
end;
procedure TNotebook.actNotebookNextExecute(Sender: TObject);
begin
Log('actNotebookNextExecute ==================================================');
SavePage;
HtTabSet1.SelectNext(false);
end;
procedure TNotebook.actNotebookRenameExecute(Sender: TObject);
var
newNotebookName: string;
currNotebookName: string;
begin
Log('actNotebookRenameExecute ================================================');
currNotebookName := notebookNames[zbiCurrentNotebook];
Log('currNotebookName: '+currNotebookName);
newNotebookName := InputBox('Rename notebook',
Format('Enter name for notebook #%d', [zbiCurrentNotebook+1]), currNotebookName);
newNotebookName := newNotebookName.Trim;
if (newNotebookName.Length > 0) then
begin
Log(Format('Rename notebook #%d to "%s"', [zbiCurrentNotebook+1, newNotebookName]));
notebookNames[zbiCurrentNotebook] := newNotebookName;
notebookNames.SaveToFile(metaFile);
LoadNotebookNames;
end;
end;
procedure TNotebook.actTabsNextExecute(Sender: TObject);
begin
Log('actTabsNextExecute ======================================================');
SavePage;
HtTabSet1.SelectNext(True);
end;
procedure TNotebook.NextPage;
begin
Log('NextPage ----------------------------------------------------------------');
SavePage;
LoadPage(zbiCurrentNotebook, zbiCurrentPage + 1);
end;
procedure TNotebook.ExportPage;
begin
Log('ExportPage --------------------------------------------------------------');
exportDlg.InitialDir := ExtractFilePath(metaFile);
if exportDlg.Execute then
begin
HtmlEditor1.SaveToFile(exportDlg.FileName);
end;
end;
procedure TNotebook.ImportPage;
begin
Log('ImportPage --------------------------------------------------------------');
ShowMessage('Import page!');
end;
procedure TNotebook.PrevPage;
begin
Log('PrevPage ----------------------------------------------------------------');
SavePage;
if zbiCurrentPage > 0 then
begin
LoadPage(zbiCurrentNotebook, zbiCurrentPage - 1);
end;
end;
procedure TNotebook.Log(message: string; alsoShowMessage: boolean = false);
begin
ramLog.AddInfo('[' + DateTimeToStr(Now) + '] ' + message);
ramLog.SaveAsTextFile(AppDataDir+'log.txt');
if bDebugMode then
StatusBar1.Panels[STATUSPANEL_STATE].Text := message;
if alsoShowMessage then
ShowMessage(message);
end;
procedure TNotebook.lstPagesClick(Sender: TObject);
begin
Log('lstPagesClick ===========================================================');
Log(Format('lstPagesClick ItemIndex=%d', [lstPages.ItemIndex]));
if loadingMode.Length > 0 then begin
Log(Format('lstPagesClick ignored due to loading mode "%s"', [loadingMode]));
Exit;
end;
FileListBoxEx1.ItemIndex := lstPages.ItemIndex;
FileListBoxEx1Change(FileListBoxEx1);
end;
procedure TNotebook.lstPagesDblClick(Sender: TObject);
begin
Log('lstPagesDblClick =========================================================');
Log(Format('lstPagesDblClick ItemIndex=%d', [lstPages.ItemIndex]));
if loadingMode.Length > 0 then begin
Log(Format('lstPagesDblClick ignored due to loading mode "%s"', [loadingMode]));
Exit;
end;
FileListBoxEx1.ItemIndex := lstPages.ItemIndex;
FileListBoxEx1Change(FileListBoxEx1);
end;
procedure TNotebook.lstPagesKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
Log('lstPagesKeyDown =========================================================');
Log(Format('lstPagesKeyDown( IGNORED! ) ItemIndex=%d', [lstPages.ItemIndex]));
{ Log(Format('lstPagesKeyUp ItemIndex=%d', [lstPages.ItemIndex]));
FileListBoxEx1.ItemIndex := lstPages.ItemIndex;
FileListBoxEx1Change(FileListBoxEx1);
}
end;
procedure TNotebook.lstPagesKeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
Log('lstPagesKeyUp ===========================================================');
if loadingMode.Length > 0 then Exit;
Log(Format('lstPagesKeyUp ItemIndex=%d', [lstPages.ItemIndex]));
FileListBoxEx1.ItemIndex := lstPages.ItemIndex;
FileListBoxEx1Change(FileListBoxEx1);
end;
procedure TNotebook.lstPagesMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Log('lstPagesMouseDown =======================================================');
Log(Format('lstPagesMouseDown ItemIndex=%d', [lstPages.ItemIndex]));
if loadingMode.Length > 0 then begin
Log(Format('lstPagesMouseDown ignored due to loading mode "%s"', [loadingMode]));
Exit;
end;
end;
procedure TNotebook.LoadPage(zbiNewNotebook: ZeroBasedInteger;
zbiNewPage: ZeroBasedInteger = 0);
var
notebookDir: string;
pageFile: string;
begin
Log('LoadPage ----------------------------------------------------------------');
Log(Format('LoadPage(%d, %d)', [zbiNewNotebook, zbiNewPage]));
// Call backend function which is testable to do actual work
GetPage(zbiNewNotebook, zbiNewPage);
end;
{
get and return a page's contents to the caller. if the page does not exist,
returns a blank string
}
function TNotebook.GetPage(AzbiNotebook: ZeroBasedInteger;
AzbiPage: ZeroBasedInteger = 0): string;
var
notebookDir: string;
pageFile: string;
begin
Log('LoadPage ----------------------------------------------------------------');
Log(Format('LoadPage(%d, %d)', [AzbiNotebook, AzbiPage]));
// Temporarily set the new notebook and page for directory creation
zbiCurrentNotebook := AzbiNotebook;
zbiCurrentPage := AzbiPage;
// Ensure new notebook directory exists
notebookDir := GetNotebookDir;
notebookDir := Format('%sNotebook%d\', [AppDataDir, AzbiNotebook + 1]);
TDirectory.CreateDirectory(notebookDir);
Log(Format('LoadPage::notebookDir=%s', [notebookDir]));
if not FileListBoxEx1.Directory.ToUpper.Equals(notebookDir.ToUpper) then
begin
Log('New directory selected, loading file list: "'
+FileListBoxEx1.Directory.ToUpper + '" != "'
+notebookDir.ToUpper + '"');
FileListBoxEx1.Directory := notebookDir;
end;
// Determine the page file
pageFile := notebookDir + GetPageFilename(AzbiPage);
Log(Format('LoadPage::pageFile=%s', [pageFile]));
EnterLoadingMode('LoadPage');
// Check if the page file we calculated above exists or not, then either
// load it or clear the editor. On first start of the program, load
// default text into Notebook 1, Page 1.
if TFile.Exists(pageFile) then
begin
Log(Format('LoadPage::LoadFromFile=%s', [pageFile]));
Result := TFile.ReadAllText(pageFile);
HtmlEditor1.LoadFromFile(pageFile);
HtmlEditor1.Visible := true;
end
else
begin
Log('LoadPage::No file found, clear editor');
HtmlEditor1.Visible := true;
HtmlEditor1.SelectAll;
HtmlEditor1.DeleteSelection;
if (AzbiNotebook = 0) and (AzbiPage = 0) then
begin
Log('LoadPage::Initialize default Notebook 1, Page 1 content');
HtmlEditor1.LoadFromString(
StrDefaultPage1_Begin+
StrDefaultPage1_Line2+
StrDefaultPage1_Line3+
StrDefaultPage1_Line4+
StrDefaultPage1_Line5+
StrDefaultPage1_Line6+
StrDefaultPage1_End)
end;
end;
Log(Format('LoadPage::Set flags to NORMAL mode: '+
'zbiCurrentNotebook=%d, zbiCurrentPage=%d',
[zbiCurrentNotebook, zbiCurrentPage]));
// Now set current notebook since we have finished loading
zbiCurrentNotebook := AzbiNotebook;
zbiCurrentPage := AzbiPage;
Log(Format('LoadPage::lstPages.ItemIndex( currently is )=%d', [lstPages.ItemIndex]));
FileListBoxEx1.ItemIndex := AzbiPage;
lstPages.ItemIndex := AzbiPage;
Log(Format('LoadPage::lstPages.ItemIndex( new value is )=%d', [lstPages.ItemIndex]));
Statusbar1.Panels[STATUSPANEL_CURRENT_LOCATION].Text := (
Format('Notebook %d, page %d',
[zbiCurrentNotebook + 1, zbiCurrentPage + 1]));
Log(Format('LoadPage::Updated statusbar panel #%d text for current page: "%s"',
[STATUSPANEL_CURRENT_LOCATION,
Statusbar1.Panels[STATUSPANEL_CURRENT_LOCATION].Text]));
// We must leave this loading mode because RefreshPagesListBox also
// uses a loading mode, which would cause a hault when we attempt
// to leave our loading mode since it would clear the mode flag.
// We could use a stack of modes instead but we are effectively done
// anyway with our own loading so we can pass it on to that proc now.
LeaveLoadingMode('LoadPage');
if lstPages.ItemIndex > -1 then
RefreshPagesListBox(lstPages.Items[lstPages.ItemIndex])
else
RefreshPagesListBox();
end;
{
save a page body to a particular notebook page. if the page does not exist,
a new filename will be created. the filename saved is returned in either
case, or a blank string if the save failed.
}
function TNotebook.PutPage(AzbiNotebook: ZeroBasedInteger;
AzbiPage: ZeroBasedInteger;
ABody: string;
AUseEditorFlag: boolean = false): string;
var
notebookDir: string;
pageFile: string;
begin
Log('PutPage ----------------------------------------------------------------');
Log(Format('PutPage AzbiNotebook=%d AzbiPage=%d ABody=%s', [AzbiNotebook, AzbiPage, ABody]));
notebookDir := GetNotebookDir(AzbiNotebook);
pageFile := notebookDir + GetPageFilename(AzbiPage);
// Save current page to directory above
Log(Format('SavePage::notebookDir=%s', [notebookDir]));
Log(Format('SavePage::pageFile=%s', [pageFile]));
if AUseEditorFlag then
HtmlEditor1.SavetoFile(pageFile)
else
TFile.WriteAllText(pageFile, ABody);
end;
function TNotebook.GetNotebookDir(zbiNotebook: ZeroBasedInteger = -1): string;
begin
if zbiNotebook = -1 then zbiNotebook := zbiCurrentNotebook;
// Ensure current notebook dir exists
Result := Format('%sNotebook%d\', [AppDataDir, zbiNotebook + 1]);
TDirectory.CreateDirectory(Result);
Log(Format('GetNotebookDir(zbiNotebook=%d)=%s', [zbiNotebook, Result]));
end;
function TNotebook.GetPageFilename(zbiPage: ZeroBasedInteger): string;
var
newFile: TextFile;
fullFilename: string;
begin
Log(Format('GetPageFilename(zbiPage=%d)', [zbiPage]));
if (FileListBoxEx1.Count = 0) or (zbiPage > FileListBoxEx1.Count - 1) then
begin
Log(Format('Index out of range %d-%d, initialize new page', [0, FileListBoxEx1.Count-1]));
Result := Format('New Page [%x].html', [HourOf(Now)*86400+MinuteOf(Now)*3600+SecondOf(Now)]);
fullFilename := GetNotebookDir + Result;
// Create the file
AssignFile(newFile, fullFilename);
Rewrite(newFile);
CloseFile(newFile);
Append(newFile);
CloseFile(newFile);
end else begin
Result := FileListBoxEx1.Items[zbiPage];
end;
Log(Format('GetPageFilename(zbiPage=%d)=%s', [zbiPage, Result]));
end;
procedure TNotebook.SavePage;
var
notebookDir: string;
pageFile: string;
begin
Log('SavePage ----------------------------------------------------------------');
// Call testable backend function to do work. Last param tells it to use
// the editor's save function instead of the body string we passed
PutPage(zbiCurrentNotebook, zbiCurrentpage, '', true);
end;
procedure TNotebook.StatusBar1Click(Sender: TObject);
begin
Log('StatusBar1Click');
if richLog.Visible then
begin
bDebugMode := false;
richLog.Visible := false;
FileListBoxEx1.Visible := false;
//memNotebookMeta.Visible := false;
end else begin
bDebugMode := not bDebugMode;
end;
if bDebugMode then
Log('Debug mode enabled. Click to disable. Double click to show full log view.')
else
Log('Debug mode disabled. Click to re-enable.');
end;
procedure TNotebook.StatusBar1DblClick(Sender: TObject);
begin
Log('StatusBar1DblClick');
richLog.Visible := true;
FileListBoxEx1.Visible := true;
lstNotebookNames.Visible := true;
bDebugMode := true;
if richLog.Visible then
Log('Full log view enabled. Click status bar to disable')
else
Log('Full log view disabled.');
end;
procedure TNotebook.LaunchBrowser(URL: string);
begin
Log('LaunchBrowser');
URL := StringReplace(URL, '"', '%22', [rfReplaceAll]);
ShellExecute(0, 'open', PChar(URL), nil, nil, SW_SHOWNORMAL);
end;
procedure TNotebook.RefreshPagesListBox(findFilename: string = '<:NONE:>');
var
zbiCurrentNotebookWas, zbiCurrentPageWas: integer;
currentPageNameWas: string;
i: integer;
bFoundPage: boolean;
begin
Log(Format('RefreshPagesListBox zbiCurrentNotebook=%d, zbiCurrentPage=%d',
[zbiCurrentNotebook, zbiCurrentPage]));
zbiCurrentNotebookWas := zbiCurrentNotebook;
zbiCurrentPageWas := zbiCurrentPage;
if zbiCurrentPage > -1 then
currentPageNameWas := findFilename
else
currentPageNameWas := '<:NONE:>';
EnterLoadingMode('RefreshPagesListBox');
// First, refresh the backing file list
// Silly mask that'll never match to
FileListBoxEx1.Mask := '*.refresh';
// force a refresh when we set real one
FileListBoxEx1.Mask := '*.html';
// Now, refresh the sorted view list
lstPages.Items := FileListBoxEx1.Items;
zbiCurrentNotebook := zbiCurrentNotebookWas;
// Instead of restoring zbiCurrentPageWas, search for the filename we had
// selected in case it has moved position
for i := 0 to FileListBoxEx1.Count -1 do
begin
if FileListBoxEx1.Items[i].Equals(currentPageNameWas) then
begin
Log(Format('Found page "%s" at index %d', [currentPageNameWas, i]));
zbiCurrentPage := i;
bFoundPage := true;
Break;
end;
end;
if bFoundPage then
lstPages.ItemIndex := zbiCUrrentPage
else
Log(Format('Page "%s" was not found in the new pages list. Was deleted?', [currentPageNameWas]));
Log(Format('RefreshPagesListBox restoring indicies to zbiCurrentNotebook=%d, zbiCurrentPage=%d',
[zbiCurrentNotebook, zbiCurrentPage]));
LeaveLoadingMode('RefreshPagesListBox');
end;
procedure TNotebook.EnterLoadingMode(modeName: string);
begin
// To notify other events that we are loading, set loadingMode
// so that other events don't accidentally replace something when
// we start modifying values in a moment
if not loadingMode.Equals('') then begin
Log(Format('Mode mismatch while entering loading mode "%s"!'#13#10+
'Expected "%s" but we are in mode "%s". Program must exit.',
[modeName, '', loadingMode]), true);
Halt;
end;
loadingMode := modeName;
Log(Format('<<<<<<<<<< loadingMode="%s"', [loadingMode]));
end;
procedure TNotebook.LeaveLoadingMode(modeName: string);
begin
if loadingMode.Equals(modeName) then
begin
Log(Format('RESET loadingMode="%s" >>>>>', [loadingMode]));
loadingMode := '';
Log(Format('loadingMode="%s"', [loadingMode]));
end else begin
Log(Format('Mode mismatch while exiting loading mode!'#13#10+
'Expected "%s" but we are in mode "%s". Program must exit.',
[modeName, loadingMode]), true);
Halt;
end;
end;
//function TNotebook.GetJsonMeta(var json: HCkJsonObject; fieldName: string; var success: boolean): string;
//begin
// // Load once if the same reference is used
// if json = nil then
// begin
// json := CkJsonObject_Create;
// CkJsonObject_putEmitCompact(json, False);
// success := CkJsonObject_Load(json, PChar(memNotebookMeta.Lines.Text));
// if (success <> True) then
// begin
// Log('Error loading JSON meta data:' + CkJsonObject__lastErrorText(json));
// end;
// end;
//
// if json <> nil then
// begin
// Result := CkJsonObject__stringOf(json, PChar(fieldName));
// end;
//end;
//
//procedure TNotebook.SetJsonMeta(json: HCkJsonObject; fieldName: string; fieldValue: string);
//begin
// CkJsonObject_SetStringOf(json, PChar(fieldName), PChar(fieldValue));
// CkJsonObject_putEmitCompact(json, False);
// // If bare-LF line endings are desired, turn off EmitCrLf
// // Otherwise CRLF line endings are emitted.
// CkJsonObject_putEmitCrLf(json, False);
// // Emit the formatted JSON:
// memNotebookMeta.Lines.Text := CkJsonObject__emit(json);
// Log(Format('Saving meta data file to "%s"', [metaFile]));
// memNotebookMeta.SaveToJSONFile(metaFile);
//end;
procedure TNotebook.LoadNotebookNames;
var
success: Boolean;
newNotebookName: string;
currNotebookName: string;
fieldName: string;
fieldValue: string;
i: Integer;
begin
Log('LoadNotebookNames =======================================================');
lstNotebookNames.Items := notebookNames;
// Field to fetch notebook name from in JSON meta object
for i := 1 to 10 do
begin
HtTabSet1.Tabs[i-1] := notebookNames[i-1]
end;
end;
procedure TNotebook.tSaveTimer(Sender: TObject);
begin
Log('tSaveTimer');
SavePage;
end;
procedure TNotebook.FileListBoxEx1Change(Sender: TObject);
begin
// @TODO: Should this event be on lstPages instead? Not sure...
// Loading mode is not zero len if we are loading a list of pages or
// other such thing and should ignore events. ItemIndex is -1 if
// there is no selection.
if (loadingMode.Length = 0) and (FileListBoxEx1.ItemIndex > -1) then
begin
Log('FileListBoxEx1Change, not ignored');
if zbiCurrentPage <> FileListBoxEx1.ItemIndex then
begin
// This would clobber a file if we had just deleted or renamed one and
// the sort order changed
SavePage;
end;
LoadPage(zbiCurrentNotebook, FileListBoxEx1.ItemIndex);
end else begin
Log('FileListBoxEx1Change, ignored due to loading');
end;
end;
procedure TNotebook.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Log('FormClose ===============================================================');
SavePage;
end;
procedure TNotebook.FormCreate(Sender: TObject);
var
//glob: HCkGlobal;
success: boolean;
begin
richLog := TRichLog.Create(self);
richLog.SetParentComponent(self);
richLog.Width := 200;
richLog.Height := 200;
richLog.ReadOnly := true;
richLog.Visible := false;
richLog.Font.Name := 'Consolas';
richLog.Font.Size := 12;
ramLog := TRamLog.Create;
ramLog.RichLog := richLog;
// Crete database directory (needed to write log output)
AppDataDir := Format('%s\mvNotebook\', [TPath.GetDocumentsPath]);
TDirectory.CreateDirectory(AppDataDir);
Log('Starting app');
// If off, log lines don't appear in status bar
bDebugMode := false;
{
@Todo: Convert to ActiveX activation
glob := CkGlobal_Create();
success := CkGlobal_UnlockBundle(glob,'Anything for 30-day trial');
if (success <> True) then
begin
ShowMessage(Format(StrUnableToLoadChikc, [CkGlobal__lastErrorText(glob)]));
Halt;
end;
}
zbiCurrentNotebook := 0;
zbiCurrentPage := 0;
metaFile := AppDataDir + 'notebooks.txt';
FreeAndNil(notebookNames);
notebookNames := TStringList.Create;
if FileExists(metaFile) then
begin
Log(Format('Loading meta data file from "%s"', [metaFile]));
notebookNames.LoadFromFile(metaFile);
end;
while notebookNames.Count < 10 do
begin
notebookNames.Add(Format('Notebook %d', [notebookNames.Count+1]));
end;
LoadNotebookNames;
LoadPage(0);
end;
procedure TNotebook.FormResize(Sender: TObject);
begin
richLog.Top := StatusBar1.Top - richLog.Height;
richLog.Width := self.Width;
FileListBoxEx1.Top := richLog.Top - FileListBoxEx1.Height - 5;
lstNotebookNames.Top := FileListBoxEx1.Top;
StatusBar1.Panels[STATUSPANEL_STATE].Width := Round(self.Width * 0.75);
end;
procedure TNotebook.FormShow(Sender: TObject);
begin
//memNotebookMeta.Visible := false;
end;
procedure TNotebook.HtmlEditor1UrlClick(Sender: TElement);
begin
Log('HtmlEditor1UrlClick -----------------------------------------------------');
LaunchBrowser(Sender['href']);
end;
procedure TNotebook.HtTabSet1Change(Sender: TObject; NewTab: Integer;
var AllowChange: Boolean);
begin
Log('HtTabSet1Change =========================================================');
SavePage;
// Takes only the tab to load for now, defaults to page 1
LoadPage(NewTab);
end;
end.