-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.py
More file actions
1223 lines (1015 loc) · 53.6 KB
/
manager.py
File metadata and controls
1223 lines (1015 loc) · 53.6 KB
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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import wx
import json
import os
import webbrowser
from pathlib import Path
import html
import shutil
class LinkData:
def __init__(self, url, label, about="", subcategory=""):
self.url = url
self.label = label
self.about = about
self.subcategory = subcategory
class LinkManagerFrame(wx.Frame):
def __init__(self):
super().__init__(None, title="Link Manager", size=(800, 600))
# Create data directory if it doesn't exist
self.data_dir = Path("data")
self.data_dir.mkdir(exist_ok=True)
self.InitUI()
self.LoadCategories()
# Focus on the treeview when app opens
self.tree.SetFocus()
self.Centre()
def InitUI(self):
# Create menu bar
menubar = wx.MenuBar()
# File menu
file_menu = wx.Menu()
add_category = file_menu.Append(wx.ID_ANY, "Add Category\tCtrl+N")
file_menu.AppendSeparator()
export_all = file_menu.Append(wx.ID_ANY, "Export All to HTML\tCtrl+Shift+E")
file_menu.AppendSeparator()
exit_item = file_menu.Append(wx.ID_EXIT, "Exit\tCtrl+Q")
menubar.Append(file_menu, "&File")
self.SetMenuBar(menubar)
# Bind menu events
self.Bind(wx.EVT_MENU, self.OnAddCategory, add_category)
self.Bind(wx.EVT_MENU, self.OnExportAll, export_all)
self.Bind(wx.EVT_MENU, self.OnExit, exit_item)
# Create main panel
panel = wx.Panel(self)
main_sizer = wx.BoxSizer(wx.VERTICAL)
# Create toolbar with just Add Category button
toolbar_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.btn_add_category = wx.Button(panel, label="Add Category")
toolbar_sizer.Add(self.btn_add_category, 0, wx.ALL, 5)
# Create tree control (standard TreeCtrl)
self.tree = wx.TreeCtrl(panel, style=wx.TR_DEFAULT_STYLE | wx.TR_HIDE_ROOT)
self.tree_root = self.tree.AddRoot("Link Categories")
# Add placeholder if no categories
self.placeholder_item = None
# Create info panel
info_panel = wx.Panel(panel)
info_sizer = wx.BoxSizer(wx.VERTICAL)
self.info_label = wx.StaticText(info_panel, label="Select a link to see details")
self.info_url = wx.TextCtrl(info_panel, style=wx.TE_READONLY)
self.about_label = wx.StaticText(info_panel, label="About:", name="about_label")
self.info_about = wx.TextCtrl(info_panel, style=wx.TE_MULTILINE|wx.TE_READONLY, size=(-1, 100))
info_sizer.Add(self.info_label, 0, wx.ALL|wx.EXPAND, 5)
info_sizer.Add(wx.StaticText(info_panel, label="URL:"), 0, wx.ALL, 5)
info_sizer.Add(self.info_url, 0, wx.ALL|wx.EXPAND, 5)
info_sizer.Add(self.about_label, 0, wx.ALL, 5)
info_sizer.Add(self.info_about, 1, wx.ALL|wx.EXPAND, 5)
info_panel.SetSizer(info_sizer)
# Add to main sizer
main_sizer.Add(toolbar_sizer, 0, wx.EXPAND)
main_sizer.Add(wx.StaticLine(panel), 0, wx.EXPAND|wx.ALL, 5)
main_sizer.Add(self.tree, 1, wx.EXPAND|wx.ALL, 5)
main_sizer.Add(info_panel, 0, wx.EXPAND|wx.ALL, 5)
panel.SetSizer(main_sizer)
# Create context menus
self.CreateContextMenus()
# Bind events
self.btn_add_category.Bind(wx.EVT_BUTTON, self.OnAddCategory)
self.tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnTreeSelectionChanged)
self.tree.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.OnTreeItemActivated)
self.tree.Bind(wx.EVT_TREE_ITEM_RIGHT_CLICK, self.OnTreeRightClick)
self.tree.Bind(wx.EVT_TREE_ITEM_MENU, self.OnTreeContextMenu)
def CreateContextMenus(self):
"""Create context menus for categories and links"""
# Category context menu
self.category_menu = wx.Menu()
self.menu_add_link = self.category_menu.Append(wx.ID_ANY, "Add Link")
self.menu_add_subcategory = self.category_menu.Append(wx.ID_ANY, "Add Subcategory")
self.category_menu.AppendSeparator()
self.menu_rename_category = self.category_menu.Append(wx.ID_ANY, "Rename Category")
self.menu_export_category = self.category_menu.Append(wx.ID_ANY, "Export to HTML")
self.category_menu.AppendSeparator()
self.menu_remove_category = self.category_menu.Append(wx.ID_ANY, "Remove Category")
# Subcategory context menu
self.subcategory_menu = wx.Menu()
self.menu_add_link_to_subcategory = self.subcategory_menu.Append(wx.ID_ANY, "Add Link")
self.subcategory_menu.AppendSeparator()
self.menu_rename_subcategory = self.subcategory_menu.Append(wx.ID_ANY, "Rename Subcategory")
self.menu_remove_subcategory = self.subcategory_menu.Append(wx.ID_ANY, "Remove Subcategory")
# Link context menu
self.link_menu = wx.Menu()
self.menu_open_link = self.link_menu.Append(wx.ID_ANY, "Open in Browser")
self.link_menu.AppendSeparator()
self.menu_edit_link = self.link_menu.Append(wx.ID_ANY, "Edit Link")
self.menu_remove_link = self.link_menu.Append(wx.ID_ANY, "Remove Link")
def LoadCategories(self):
"""Load all categories from JSON files"""
self.tree.DeleteChildren(self.tree_root)
self.placeholder_item = None
# Get all JSON files in the main data directory (categories)
json_files = list(self.data_dir.glob("*.json"))
if not json_files:
# Show placeholder if no categories
self.placeholder_item = self.tree.AppendItem(self.tree_root, "No categories")
self.tree.SetItemTextColour(self.placeholder_item, wx.Colour(128, 128, 128))
self.tree.SetItemData(self.placeholder_item, {"type": "placeholder"})
return
# Process main category files
for json_file in json_files:
category_name = json_file.stem
category_item = self.tree.AppendItem(self.tree_root, category_name)
self.tree.SetItemData(category_item, {"type": "category", "filename": json_file.name})
self.tree.SetItemBold(category_item, True)
# Load links from JSON file
try:
with open(json_file, 'r', encoding='utf-8') as f:
links = json.load(f)
# Add direct links to category
sorted_links = sorted(links.items(), key=lambda x: x[1].get("label", x[0]).lower())
for url, data in sorted_links:
link_item = self.tree.AppendItem(category_item, data.get("label", url))
link_data = LinkData(
url,
data.get("label", url),
data.get("about", ""),
"" # No subcategory for main files
)
self.tree.SetItemData(link_item, {"type": "link", "data": link_data})
# Check if this category has a subcategory folder
subcat_folder = self.data_dir / category_name
if subcat_folder.exists() and subcat_folder.is_dir():
# Load subcategories from the folder
subcategory_files = list(subcat_folder.glob("*.json"))
for subcategory_file in subcategory_files:
subcategory_name = subcategory_file.stem
subcategory_item = self.tree.AppendItem(category_item, subcategory_name)
self.tree.SetItemData(subcategory_item, {
"type": "subcategory",
"name": subcategory_name,
"filename": subcategory_file.name
})
# Load links from subcategory file
try:
with open(subcategory_file, 'r', encoding='utf-8') as f:
subcat_links = json.load(f)
# Sort links
sorted_subcat_links = sorted(subcat_links.items(), key=lambda x: x[1].get("label", x[0]).lower())
for suburl, subdata in sorted_subcat_links:
sublink_item = self.tree.AppendItem(subcategory_item, subdata.get("label", suburl))
sublink_data = LinkData(
suburl,
subdata.get("label", suburl),
subdata.get("about", ""),
subcategory_name
)
self.tree.SetItemData(sublink_item, {"type": "link", "data": sublink_data})
except (json.JSONDecodeError, FileNotFoundError):
pass
except (json.JSONDecodeError, FileNotFoundError):
pass
def SortCategoryLinks(self, category_item):
"""Sort links in a category alphabetically"""
# Collect all link items and their data
links_data = []
subcategories = []
child, cookie = self.tree.GetFirstChild(category_item)
while child.IsOk():
child_data = self.tree.GetItemData(child)
if child_data:
if child_data.get("type") == "link":
link_data = child_data.get("data")
if link_data:
links_data.append((child, link_data))
elif child_data.get("type") == "subcategory":
subcategories.append(child)
child, cookie = self.tree.GetNextChild(category_item, cookie)
# Sort by label
links_data.sort(key=lambda x: x[1].label.lower())
# Remove direct links (keep subcategories)
for child, _ in links_data:
self.tree.Delete(child)
# Re-add in sorted order
for _, link_data in links_data:
link_item = self.tree.AppendItem(category_item, link_data.label)
self.tree.SetItemData(link_item, {"type": "link", "data": link_data})
def SortSubcategoryLinks(self, subcategory_item):
"""Sort links in a subcategory alphabetically"""
# Collect all link items and their data
links_data = []
child, cookie = self.tree.GetFirstChild(subcategory_item)
while child.IsOk():
child_data = self.tree.GetItemData(child)
if child_data and child_data.get("type") == "link":
link_data = child_data.get("data")
if link_data:
links_data.append((child, link_data))
child, cookie = self.tree.GetNextChild(subcategory_item, cookie)
# Sort by label
links_data.sort(key=lambda x: x[1].label.lower())
# Delete all children
self.tree.DeleteChildren(subcategory_item)
# Re-add in sorted order
for _, link_data in links_data:
link_item = self.tree.AppendItem(subcategory_item, link_data.label)
self.tree.SetItemData(link_item, {"type": "link", "data": link_data})
def SaveCategory(self, category_item):
"""Save a category to its JSON file"""
item_data = self.tree.GetItemData(category_item)
if not item_data or item_data.get("type") != "category":
return
category_name = self.tree.GetItemText(category_item)
filename = item_data.get("filename")
if not filename:
filename = f"{category_name}.json"
item_data["filename"] = filename
filepath = self.data_dir / filename
# Collect direct links in this category
direct_links = {}
# Process direct children of category
child, cookie = self.tree.GetFirstChild(category_item)
while child.IsOk():
child_data = self.tree.GetItemData(child)
if child_data:
if child_data.get("type") == "link":
link_data = child_data.get("data")
if link_data:
direct_links[link_data.url] = {
"label": link_data.label,
"about": link_data.about,
}
elif child_data.get("type") == "subcategory":
# Save subcategory separately
self.SaveSubcategory(child, category_name)
child, cookie = self.tree.GetNextChild(category_item, cookie)
# Save main category file
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(direct_links, f, indent=4, ensure_ascii=False)
def SaveSubcategory(self, subcategory_item, parent_category_name):
"""Save a subcategory to its own JSON file in the parent category folder"""
item_data = self.tree.GetItemData(subcategory_item)
if not item_data or item_data.get("type") != "subcategory":
return
subcategory_name = self.tree.GetItemText(subcategory_item)
# Create/ensure parent category folder exists
category_folder = self.data_dir / parent_category_name
if not category_folder.exists():
category_folder.mkdir(exist_ok=True)
# Define subcategory file path
subcategory_file = category_folder / f"{subcategory_name}.json"
# Collect links in this subcategory
links = {}
child, cookie = self.tree.GetFirstChild(subcategory_item)
while child.IsOk():
child_data = self.tree.GetItemData(child)
if child_data and child_data.get("type") == "link":
link_data = child_data.get("data")
if link_data:
links[link_data.url] = {
"label": link_data.label,
"about": link_data.about,
}
child, cookie = self.tree.GetNextChild(subcategory_item, cookie)
# Save subcategory file
with open(subcategory_file, 'w', encoding='utf-8') as f:
json.dump(links, f, indent=4, ensure_ascii=False)
# Update item data with filename
item_data["filename"] = subcategory_file.name
def RemovePlaceholder(self):
"""Remove the 'No categories' placeholder if it exists"""
if self.placeholder_item and self.placeholder_item.IsOk():
self.tree.Delete(self.placeholder_item)
self.placeholder_item = None
def OnTreeRightClick(self, event):
"""Handle right-click on tree item"""
item = event.GetItem()
if not item.IsOk():
return
self.tree.SelectItem(item)
self.OnTreeContextMenu(event)
def OnTreeContextMenu(self, event):
"""Show context menu for tree item"""
selected = self.tree.GetSelection()
if not selected.IsOk():
return
item_data = self.tree.GetItemData(selected)
if not item_data:
return
item_type = item_data.get("type")
if item_type == "category":
# Bind category menu events
self.Bind(wx.EVT_MENU, lambda e: self.OnAddLink(e, selected), self.menu_add_link)
self.Bind(wx.EVT_MENU, lambda e: self.OnAddSubcategory(e, selected), self.menu_add_subcategory)
self.Bind(wx.EVT_MENU, lambda e: self.OnRenameCategory(e, selected), self.menu_rename_category)
self.Bind(wx.EVT_MENU, lambda e: self.OnExportCategory(e, selected), self.menu_export_category)
self.Bind(wx.EVT_MENU, lambda e: self.OnRemoveCategory(e, selected), self.menu_remove_category)
self.PopupMenu(self.category_menu)
elif item_type == "subcategory":
# Bind subcategory menu events
self.Bind(wx.EVT_MENU, lambda e: self.OnAddLinkToSubcategory(e, selected), self.menu_add_link_to_subcategory)
self.Bind(wx.EVT_MENU, lambda e: self.OnRenameSubcategory(e, selected), self.menu_rename_subcategory)
self.Bind(wx.EVT_MENU, lambda e: self.OnRemoveSubcategory(e, selected), self.menu_remove_subcategory)
self.PopupMenu(self.subcategory_menu)
elif item_type == "link":
# Bind link menu events
self.Bind(wx.EVT_MENU, lambda e: self.OnOpenLink(e, selected), self.menu_open_link)
self.Bind(wx.EVT_MENU, lambda e: self.OnEditLink(e, selected), self.menu_edit_link)
self.Bind(wx.EVT_MENU, lambda e: self.OnRemoveLink(e, selected), self.menu_remove_link)
self.PopupMenu(self.link_menu)
def OnAddCategory(self, event):
"""Add a new category"""
dialog = wx.TextEntryDialog(self, "Enter category name:", "Add Category")
if dialog.ShowModal() == wx.ID_OK:
category_name = dialog.GetValue().strip()
if not category_name:
wx.MessageBox("Category name cannot be empty!", "Error", wx.OK | wx.ICON_ERROR)
dialog.Destroy()
return
# Check if category already exists
existing = False
child, cookie = self.tree.GetFirstChild(self.tree_root)
while child.IsOk():
if self.tree.GetItemText(child) == category_name:
existing = True
break
child, cookie = self.tree.GetNextChild(self.tree_root, cookie)
if not existing:
self.RemovePlaceholder()
category_item = self.tree.AppendItem(self.tree_root, category_name)
filename = f"{category_name}.json"
self.tree.SetItemData(category_item, {"type": "category", "filename": filename})
self.tree.SetItemBold(category_item, True)
self.SaveCategory(category_item)
else:
wx.MessageBox("Category already exists!", "Error", wx.OK | wx.ICON_ERROR)
dialog.Destroy()
def OnRemoveCategory(self, event, category_item=None):
"""Remove selected category"""
if not category_item:
return
item_data = self.tree.GetItemData(category_item)
if item_data and item_data.get("type") == "category":
category_name = self.tree.GetItemText(category_item)
result = wx.MessageBox(f"Are you sure you want to delete '{category_name}' and all its links?",
"Confirm Delete", wx.YES_NO | wx.ICON_QUESTION)
if result == wx.YES:
# Delete JSON file
filename = item_data.get("filename")
if filename:
filepath = self.data_dir / filename
if filepath.exists():
filepath.unlink()
# Delete category folder if it exists
category_folder = self.data_dir / category_name
if category_folder.exists():
try:
shutil.rmtree(category_folder)
except:
pass # Ignore errors if any
# Remove from tree
self.tree.Delete(category_item)
# Check if we need to show placeholder
child, cookie = self.tree.GetFirstChild(self.tree_root)
if not child.IsOk():
self.placeholder_item = self.tree.AppendItem(self.tree_root, "No categories")
self.tree.SetItemTextColour(self.placeholder_item, wx.Colour(128, 128, 128))
self.tree.SetItemData(self.placeholder_item, {"type": "placeholder"})
def OnRenameCategory(self, event, category_item=None):
"""Rename selected category"""
if not category_item:
return
item_data = self.tree.GetItemData(category_item)
if item_data and item_data.get("type") == "category":
old_name = self.tree.GetItemText(category_item)
dialog = wx.TextEntryDialog(self, "Enter new category name:", "Rename Category", old_name)
if dialog.ShowModal() == wx.ID_OK:
new_name = dialog.GetValue().strip()
if not new_name:
wx.MessageBox("Category name cannot be empty!", "Error", wx.OK | wx.ICON_ERROR)
dialog.Destroy()
return
if new_name != old_name:
# Check if new name already exists
existing = False
child, cookie = self.tree.GetFirstChild(self.tree_root)
while child.IsOk():
if child != category_item and self.tree.GetItemText(child) == new_name:
existing = True
break
child, cookie = self.tree.GetNextChild(self.tree_root, cookie)
if not existing:
# Rename JSON file
old_filename = item_data.get("filename")
if old_filename:
old_filepath = self.data_dir / old_filename
new_filename = f"{new_name}.json"
new_filepath = self.data_dir / new_filename
if old_filepath.exists():
old_filepath.rename(new_filepath)
item_data["filename"] = new_filename
# Rename category folder if it exists
old_folder = self.data_dir / old_name
if old_folder.exists() and old_folder.is_dir():
new_folder = self.data_dir / new_name
old_folder.rename(new_folder)
# Update tree
self.tree.SetItemText(category_item, new_name)
else:
wx.MessageBox("A category with that name already exists!", "Error", wx.OK | wx.ICON_ERROR)
dialog.Destroy()
def OnAddSubcategory(self, event, category_item=None):
"""Add a new subcategory to selected category"""
if not category_item:
return
# Check if category already has a subcategory
has_subcategory = False
child, cookie = self.tree.GetFirstChild(category_item)
while child.IsOk():
child_data = self.tree.GetItemData(child)
if child_data and child_data.get("type") == "subcategory":
has_subcategory = True
break
child, cookie = self.tree.GetNextChild(category_item, cookie)
if has_subcategory:
wx.MessageBox("This category already has a subcategory. Only one subcategory per category is allowed.",
"Cannot Add Subcategory", wx.OK | wx.ICON_INFORMATION)
return
dialog = wx.TextEntryDialog(self, "Enter subcategory name:", "Add Subcategory")
if dialog.ShowModal() == wx.ID_OK:
subcategory_name = dialog.GetValue().strip()
if not subcategory_name:
wx.MessageBox("Subcategory name cannot be empty!", "Error", wx.OK | wx.ICON_ERROR)
dialog.Destroy()
return
subcategory_item = self.tree.AppendItem(category_item, subcategory_name)
self.tree.SetItemData(subcategory_item, {"type": "subcategory", "name": subcategory_name})
# Save the subcategory
category_name = self.tree.GetItemText(category_item)
self.SaveSubcategory(subcategory_item, category_name)
self.tree.Expand(category_item)
dialog.Destroy()
def OnRenameSubcategory(self, event, subcategory_item=None):
"""Rename selected subcategory"""
if not subcategory_item:
return
item_data = self.tree.GetItemData(subcategory_item)
if item_data and item_data.get("type") == "subcategory":
old_name = self.tree.GetItemText(subcategory_item)
dialog = wx.TextEntryDialog(self, "Enter new subcategory name:", "Rename Subcategory", old_name)
if dialog.ShowModal() == wx.ID_OK:
new_name = dialog.GetValue().strip()
if not new_name:
wx.MessageBox("Subcategory name cannot be empty!", "Error", wx.OK | wx.ICON_ERROR)
dialog.Destroy()
return
if new_name != old_name:
# Update tree
self.tree.SetItemText(subcategory_item, new_name)
item_data["name"] = new_name
# Update links in this subcategory
child, cookie = self.tree.GetFirstChild(subcategory_item)
while child.IsOk():
child_data = self.tree.GetItemData(child)
if child_data and child_data.get("type") == "link":
link_data = child_data.get("data")
if link_data:
link_data.subcategory = new_name
child, cookie = self.tree.GetNextChild(subcategory_item, cookie)
# Get parent category
category_item = self.tree.GetItemParent(subcategory_item)
category_name = self.tree.GetItemText(category_item)
# Rename the subcategory file
category_folder = self.data_dir / category_name
old_file = category_folder / f"{old_name}.json"
new_file = category_folder / f"{new_name}.json"
if old_file.exists():
old_file.rename(new_file)
# Update filename in item data
item_data["filename"] = f"{new_name}.json"
dialog.Destroy()
def OnRemoveSubcategory(self, event, subcategory_item=None):
"""Remove selected subcategory"""
if not subcategory_item:
return
item_data = self.tree.GetItemData(subcategory_item)
if item_data and item_data.get("type") == "subcategory":
subcategory_name = self.tree.GetItemText(subcategory_item)
result = wx.MessageBox(f"Are you sure you want to delete '{subcategory_name}' and all its links?",
"Confirm Delete", wx.YES_NO | wx.ICON_QUESTION)
if result == wx.YES:
# Get parent category
category_item = self.tree.GetItemParent(subcategory_item)
category_name = self.tree.GetItemText(category_item)
# Delete subcategory file
category_folder = self.data_dir / category_name
subcategory_file = category_folder / f"{subcategory_name}.json"
if subcategory_file.exists():
subcategory_file.unlink()
# Check if category folder is now empty and remove it if so
if category_folder.exists():
if not any(category_folder.iterdir()):
try:
category_folder.rmdir()
except:
pass # Ignore errors
# Remove from tree
self.tree.Delete(subcategory_item)
def OnAddLink(self, event, category_item=None):
"""Add a new link to selected category"""
if not category_item:
return
# Show dialog to add link
dialog = LinkDialog(self, "Add Link")
if dialog.ShowModal() == wx.ID_OK:
url, label, about = dialog.GetValues()
# URL validation is done in the dialog
# Add to tree
link_item = self.tree.AppendItem(category_item, label or url)
link_data = LinkData(url, label or url, about)
self.tree.SetItemData(link_item, {"type": "link", "data": link_data})
# Save category
self.SaveCategory(category_item)
# Sort the links in this category
self.SortCategoryLinks(category_item)
self.tree.Expand(category_item)
dialog.Destroy()
def OnAddLinkToSubcategory(self, event, subcategory_item=None):
"""Add a new link to selected subcategory"""
if not subcategory_item:
return
# Show dialog to add link
dialog = LinkDialog(self, "Add Link to Subcategory")
if dialog.ShowModal() == wx.ID_OK:
url, label, about = dialog.GetValues()
# URL validation is done in the dialog
# Add to tree
link_item = self.tree.AppendItem(subcategory_item, label or url)
# Get subcategory name
subcategory_name = self.tree.GetItemText(subcategory_item)
link_data = LinkData(url, label or url, about, subcategory_name)
self.tree.SetItemData(link_item, {"type": "link", "data": link_data})
# Save subcategory
category_item = self.tree.GetItemParent(subcategory_item)
category_name = self.tree.GetItemText(category_item)
self.SaveSubcategory(subcategory_item, category_name)
# Sort the links in this subcategory
self.SortSubcategoryLinks(subcategory_item)
self.tree.Expand(subcategory_item)
dialog.Destroy()
def OnRemoveLink(self, event, link_item=None):
"""Remove selected link"""
if not link_item:
return
item_data = self.tree.GetItemData(link_item)
if item_data and item_data.get("type") == "link":
link_data = item_data.get("data")
result = wx.MessageBox(f"Are you sure you want to delete '{link_data.label}'?",
"Confirm Delete", wx.YES_NO | wx.ICON_QUESTION)
if result == wx.YES:
parent_item = self.tree.GetItemParent(link_item)
parent_data = self.tree.GetItemData(parent_item)
# Delete the link from tree
self.tree.Delete(link_item)
if parent_data and parent_data.get("type") == "subcategory":
# Link is in a subcategory
category_item = self.tree.GetItemParent(parent_item)
category_name = self.tree.GetItemText(category_item)
self.SaveSubcategory(parent_item, category_name)
else:
# Link is directly in a category
self.SaveCategory(parent_item)
def OnEditLink(self, event, link_item=None):
"""Edit selected link"""
if not link_item:
return
item_data = self.tree.GetItemData(link_item)
if item_data and item_data.get("type") == "link":
link_data = item_data.get("data")
if link_data:
dialog = LinkDialog(self, "Edit Link",
url=link_data.url,
label=link_data.label,
about=link_data.about)
if dialog.ShowModal() == wx.ID_OK:
url, label, about = dialog.GetValues()
# URL validation is done in the dialog
# Update link data
link_data.url = url
link_data.label = label or url
link_data.about = about
# Update tree
self.tree.SetItemText(link_item, label or url)
# Update info panel if this link is selected
if self.tree.GetSelection() == link_item:
self.info_label.SetLabel(f"Link: {link_data.label}")
self.info_url.SetValue(link_data.url)
if link_data.about:
self.info_about.SetValue(link_data.about)
self.info_about.Show()
self.about_label.Show()
else:
self.info_about.Hide()
self.about_label.Hide()
self.info_about.GetParent().Layout()
# Save changes
parent_item = self.tree.GetItemParent(link_item)
parent_data = self.tree.GetItemData(parent_item)
if parent_data and parent_data.get("type") == "subcategory":
# Link is in a subcategory
category_item = self.tree.GetItemParent(parent_item)
category_name = self.tree.GetItemText(category_item)
self.SaveSubcategory(parent_item, category_name)
self.SortSubcategoryLinks(parent_item)
else:
# Link is directly in a category
self.SaveCategory(parent_item)
self.SortCategoryLinks(parent_item)
dialog.Destroy()
def OnOpenLink(self, event, link_item=None):
"""Open link in browser"""
if not link_item:
return
item_data = self.tree.GetItemData(link_item)
if item_data and item_data.get("type") == "link":
link_data = item_data.get("data")
if link_data:
webbrowser.open(link_data.url)
def OnTreeSelectionChanged(self, event):
"""Handle tree selection change"""
selected = self.tree.GetSelection()
if not selected.IsOk():
return
item_data = self.tree.GetItemData(selected)
if item_data and item_data.get("type") == "link":
link_data = item_data.get("data")
if link_data:
self.info_label.SetLabel(f"Link: {link_data.label}")
self.info_url.SetValue(link_data.url)
# Only show about section if there's content
if link_data.about:
self.info_about.SetValue(link_data.about)
self.info_about.Show()
self.about_label.Show()
else:
self.info_about.Hide()
self.about_label.Hide()
self.info_about.GetParent().Layout()
else:
self.info_label.SetLabel("Select a link to see details")
self.info_url.SetValue("")
self.info_about.Hide()
self.about_label.Hide()
self.info_about.GetParent().Layout()
def OnTreeItemActivated(self, event):
"""Handle double-click or Enter on tree item"""
selected = self.tree.GetSelection()
if not selected.IsOk():
return
item_data = self.tree.GetItemData(selected)
if item_data:
if item_data.get("type") == "link":
link_data = item_data.get("data")
if link_data:
webbrowser.open(link_data.url)
elif item_data.get("type") in ["category", "subcategory"]:
# Toggle expand/collapse
if self.tree.IsExpanded(selected):
self.tree.Collapse(selected)
else:
self.tree.Expand(selected)
def OnExportCategory(self, event, category_item=None):
"""Export current category to HTML"""
if not category_item:
return
category_name = self.tree.GetItemText(category_item)
# Ask where to save
wildcard = "HTML files (*.html)|*.html"
dialog = wx.FileDialog(self, "Export Category to HTML",
defaultFile=f"{category_name}.html",
wildcard=wildcard,
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if dialog.ShowModal() == wx.ID_OK:
filepath = dialog.GetPath()
self.ExportCategoryToHTML(category_item, filepath)
wx.MessageBox(f"Category exported to {filepath}", "Export Complete", wx.OK | wx.ICON_INFORMATION)
dialog.Destroy()
def OnExportAll(self, event):
"""Export all categories to HTML"""
# Check if there are any categories
child, cookie = self.tree.GetFirstChild(self.tree_root)
if not child.IsOk() or (self.placeholder_item and child == self.placeholder_item):
wx.MessageBox("No categories to export", "Nothing to Export", wx.OK | wx.ICON_WARNING)
return
wildcard = "HTML files (*.html)|*.html"
dialog = wx.FileDialog(self, "Export All to HTML",
defaultFile="all_links.html",
wildcard=wildcard,
style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
if dialog.ShowModal() == wx.ID_OK:
filepath = dialog.GetPath()
self.ExportAllToHTML(filepath)
wx.MessageBox(f"All categories exported to {filepath}", "Export Complete", wx.OK | wx.ICON_INFORMATION)
dialog.Destroy()
def ExportCategoryToHTML(self, category_item, filepath):
"""Export a single category to HTML file"""
category_name = self.tree.GetItemText(category_item)
html_content = f"""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{html.escape(category_name)} - Links</title>
<style>
body {{ font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; }}
h1 {{ color: #333; border-bottom: 2px solid #333; padding-bottom: 10px; }}
h2 {{ color: #555; border-bottom: 1px solid #999; padding-bottom: 5px; margin-top: 20px; }}
.links-container {{ background: white; padding: 20px; margin: 20px 0; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }}
ul {{ list-style-type: none; padding: 0; }}
li {{ margin: 15px 0; padding: 15px; background: #f8f8f8; border-left: 4px solid #0066cc; }}
.link-title {{ font-size: 18px; font-weight: bold; margin-bottom: 5px; }}
.link-url {{ color: #0066cc; text-decoration: none; }}
.link-url:hover {{ text-decoration: underline; }}
.link-about {{ color: #666; margin-top: 5px; font-style: italic; }}
</style>
</head>
<body>
<h1>{html.escape(category_name)}</h1>
"""
# Collect links and subcategories
direct_links = []
subcategories = {}
child, cookie = self.tree.GetFirstChild(category_item)
while child.IsOk():
child_data = self.tree.GetItemData(child)
if child_data:
if child_data.get("type") == "link":
link_data = child_data.get("data")
if link_data:
direct_links.append(link_data)
elif child_data.get("type") == "subcategory":
subcategory_name = self.tree.GetItemText(child)
subcategory_links = []
subchild, subcookie = self.tree.GetFirstChild(child)
while subchild.IsOk():
subchild_data = self.tree.GetItemData(subchild)
if subchild_data and subchild_data.get("type") == "link":
sublink_data = subchild_data.get("data")
if sublink_data:
subcategory_links.append(sublink_data)
subchild, subcookie = self.tree.GetNextChild(child, subcookie)
if subcategory_links:
subcategories[subcategory_name] = sorted(subcategory_links, key=lambda x: x.label.lower())
child, cookie = self.tree.GetNextChild(category_item, cookie)
# Sort direct links
direct_links.sort(key=lambda x: x.label.lower())
# Add direct links
html_content += """ <div class="links-container">
<h2>Main Links</h2>
"""
if direct_links:
html_content += " <ul>\n"
for link_data in direct_links:
html_content += f""" <li>
<a href="{html.escape(link_data.url)}" class="link-url" target="_blank">{html.escape(link_data.label)}</a>
"""
if link_data.about:
html_content += f""" <div class="link-about">{html.escape(link_data.about)}</div>
"""
html_content += """ </li>
"""
html_content += " </ul>\n"
else:
html_content += """ <p>No direct links in this category.</p>
"""
html_content += " </div>\n"
# Add subcategories
for subcategory_name, subcategory_links in subcategories.items():
html_content += f""" <div class="links-container">
<h2>{html.escape(subcategory_name)}</h2>
"""
if subcategory_links:
html_content += " <ul>\n"
for link_data in subcategory_links:
html_content += f""" <li>
<a href="{html.escape(link_data.url)}" class="link-url" target="_blank">{html.escape(link_data.label)}</a>
"""
if link_data.about:
html_content += f""" <div class="link-about">{html.escape(link_data.about)}</div>
"""
html_content += """ </li>
"""
html_content += " </ul>\n"
else:
html_content += """ <p>No links in this subcategory.</p>
"""
html_content += " </div>\n"
html_content += """</body>
</html>"""
with open(filepath, 'w', encoding='utf-8') as f:
f.write(html_content)
def ExportAllToHTML(self, filepath):
"""Export all categories to a single HTML file"""
html_content = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>All Links</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background-color: #f5f5f5; }
h1 { color: #333; border-bottom: 3px solid #333; padding-bottom: 10px; }
h2 { color: #555; border-bottom: 1px solid #999; padding-bottom: 5px; margin-top: 30px; }
h3 { color: #666; margin-top: 20px; }
.links-container { background: white; padding: 20px; margin: 20px 0; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
ul { list-style-type: none; padding: 0; }
li { margin: 15px 0; padding: 15px; background: #f8f8f8; border-left: 4px solid #0066cc; }
.link-title { font-size: 18px; font-weight: bold; margin-bottom: 5px; }
.link-url { color: #0066cc; text-decoration: none; }
.link-url:hover { text-decoration: underline; }
.link-about { color: #666; margin-top: 5px; font-style: italic; }
.toc { background: white; padding: 20px; margin: 20px 0; border-radius: 5px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
.toc h3 { margin-top: 0; }
.toc ul { list-style-type: disc; padding-left: 20px; }
.toc li { margin: 5px 0; padding: 0; background: none; border: none; }
.toc a { color: #0066cc; text-decoration: none; }
.toc a:hover { text-decoration: underline; }
.subcategory { margin-left: 20px; }
</style>
</head>
<body>