-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathYouDaoTree.cs
More file actions
1562 lines (1313 loc) · 62 KB
/
YouDaoTree.cs
File metadata and controls
1562 lines (1313 loc) · 62 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using WeifenLuo.WinFormsUI.Docking;
using System.Data.OleDb;
using System.Threading;
using System.Configuration;
using WeCode1._0.youdao;
namespace WeCode1._0
{
public partial class YouDaoTree : DockContent
{
public FormMain formParent;
TreeNode dragDropTreeNode;
DateTime startTime;
private Thread beginInvokeThread;
private delegate void beginInvokeDelegate();
private void StartMethod()
{
//授权校验
//判断token是否有效
string IsAuthor = AuthorAPI.GetIsAuthor();
if (IsAuthor != "OK")
{
//禁用云目录,授权失败
Attachment.IsTokeneffective = 0;
this.BeginInvoke(new beginInvokeDelegate(showNoAuthor));
formParent.SetAuthor(false);
}
else
{
//从云端拉取XML同步到本地
XMLAPI.Yun2XML();
Attachment.IsTokeneffective = 1;
this.BeginInvoke(new beginInvokeDelegate(beginInvokeMethod));
formParent.SetAuthor(true);
}
}
private void showNoAuthor()
{
//MessageBox.Show("未授权有道云笔记或者授权已过期,请点击用户--登录以重新授权!");
if (ConfigurationManager.AppSettings["authorAlert"] != "0")
{
if (MessageBox.Show("wecode笔记可以保存在有道云上,您可通过云笔记-登录有道云登录!\n\n点击“确定”不再提醒", "登录提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
PubFunc.SetConfiguration("authorAlert", "0");
}
}
}
//校验通过后通知本窗体加载树
private void beginInvokeMethod()
{
MakeTree();
}
public YouDaoTree()
{
InitializeComponent();
treeViewYouDao.AllowDrop = true;
}
//窗体加载
private void YouDaoTree_Load(object sender, EventArgs e)
{
//20140719 将主窗口判断授权移至这里判断
//另开线程,避免因网络原因造成的卡死,验证完成给主窗口发送消息,同时判断是否加载树
beginInvokeThread = new Thread(new ThreadStart(StartMethod));
beginInvokeThread.Start();
MakeTree();
}
//初始化
private void MakeTree()
{
//绑定树
if (Attachment.IsTokeneffective == 1)
{
treeViewYouDao.Enabled = true;
IniYoudaoTree();
}
else
{
treeViewYouDao.Enabled = false;
toolStrip1.Enabled = false;
}
}
#region 从xml加载生成树
/// <summary>
/// 从XML加载绑定树
/// </summary>
public void IniYoudaoTree()
{
this.treeViewYouDao.Enabled = true;
try
{
this.Cursor = Cursors.WaitCursor;
XmlDocument xDoc = new XmlDocument();
xDoc.Load("TreeNodeLocal.xml");
treeViewYouDao.Nodes.Clear();
treeViewYouDao.ImageList = imageList1;
XmlNode wecode = xDoc.DocumentElement;
foreach (XmlNode cNode in wecode)
{
//添加根节点
TreeNode tNode = new TreeNode();
tNode.ImageIndex = 0;
tNode.SelectedImageIndex = 0;
tNode.Text = cNode.Attributes["title"].Value.ToString();
tNode.Name = cNode.Attributes["id"].Value;
if (cNode.Name == "note")
{
treeTagNote tNoteTag = new treeTagNote();
tNoteTag.path = cNode.Attributes["path"].Value;
tNoteTag.createtime = cNode.Attributes["createtime"].Value;
tNoteTag.Language = cNode.Attributes["Language"].Value;
tNoteTag.isMark = cNode.Attributes["isMark"].Value;
tNode.ImageIndex = 1;
tNode.SelectedImageIndex = 1;
if (cNode.Attributes["IsLock"].Value == "1")
{
tNode.ImageIndex = 2;
tNode.SelectedImageIndex = 2;
}
tNode.Tag = tNoteTag;
}
else if (cNode.Name == "book")
{
treeTagBook tBookTag = new treeTagBook();
tBookTag.Language = cNode.Attributes["Language"].Value;
tNode.Tag = tBookTag;
}
treeViewYouDao.Nodes.Add(tNode);
addTreeNode(cNode, tNode);
}
//默认选中第一个节点
if (treeViewYouDao.Nodes.Count > 0)
{
treeViewYouDao.SelectedNode = treeViewYouDao.Nodes[0];
}
toolStrip1.Enabled = true;
}
catch (XmlException xExc) //Exception is thrown is there is an error in the Xml
{
MessageBox.Show(xExc.Message);
}
catch (Exception ex) //General exception
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = Cursors.Default; //Change the cursor back
}
}
//This function is called recursively until all nodes are loaded
private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
XmlNode xNode;
TreeNode tNode;
XmlNodeList xNodeList;
if (xmlNode.HasChildNodes) //The current node has children
{
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++) //Loop through the child nodes
{
xNode = xmlNode.ChildNodes[x];
TreeNode tempNode = new TreeNode();
tempNode.ImageIndex = 0;
tempNode.SelectedImageIndex = 0;
tempNode.Text = xNode.Attributes["title"].Value.ToString();
tempNode.Name = xNode.Attributes["id"].Value;
if (xNode.Name == "note")
{
treeTagNote tNoteTag = new treeTagNote();
tNoteTag.path = xNode.Attributes["path"].Value;
tNoteTag.createtime = xNode.Attributes["createtime"].Value;
tNoteTag.Language = xNode.Attributes["Language"].Value;
tempNode.ImageIndex = 1;
tempNode.SelectedImageIndex = 1;
if (xNode.Attributes["IsLock"].Value == "1")
{
tempNode.ImageIndex = 2;
tempNode.SelectedImageIndex = 2;
}
tempNode.Tag = tNoteTag;
}
else if (xNode.Name == "book")
{
treeTagBook tBookTag = new treeTagBook();
tBookTag.Language = xNode.Attributes["Language"].Value;
tempNode.Tag = tBookTag;
}
treeNode.Nodes.Add(tempNode);
tNode = treeNode.Nodes[x];
addTreeNode(xNode, tNode);
}
}
}
#endregion
//双击打开文章
private void treeViewYouDao_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (treeViewYouDao.SelectedNode == null)
return;
int iType = treeViewYouDao.SelectedNode.ImageIndex;
if (iType == 0)
{
//双击目录
}
else
{
//双击文章,如果已经打开,则定位,否则新窗口打开
string sNodeId = ((treeTagNote)treeViewYouDao.SelectedNode.Tag).path;
string sLang = ((treeTagNote)treeViewYouDao.SelectedNode.Tag).Language;
//updateTime = "最后更新时间:" + PubFunc.seconds2Time(Convert.ToInt32(updateTime)).ToString();
string treeLocation = treeViewYouDao.SelectedNode.FullPath;
if (iType == 2)
{
//加密,对content解密
string MykeydYd = "";
if (Attachment.KeyDYouDao != "")
{
//内存中已存在秘钥
MykeydYd = Attachment.KeyDYouDao;
}
else
{
//内存中不存在秘钥
DialogPSWYouDao dp = new DialogPSWYouDao("3");
DialogResult dr = dp.ShowDialog();
if (dr == DialogResult.OK)
{
MykeydYd = dp.ReturnVal;
}
}
if (MykeydYd == "")
return;
}
formParent.openNewYouDao(sNodeId, treeViewYouDao.SelectedNode.Text,treeLocation,iType);
///打开后设置语言
string Language = PubFunc.Synid2LanguageSetLang(PubFunc.Language2Synid(sLang));
if (Attachment.isnewOpenDoc == "1")
{
formParent.SetLanguage(Language);
}
}
}
//新建目录
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
//获取选中节点
TreeNode SeleNode = treeViewYouDao.SelectedNode;
string isCreateRoot = "False";
string ParLang = "TEXT";
//如果没有选中节点,则新建顶层目录
if (SeleNode == null || treeViewYouDao.Nodes.Count == 0)
{
//没有节点
isCreateRoot = "True";
}
else
{
if (SeleNode.ImageIndex == 0)
{
ParLang = ((treeTagBook)SeleNode.Tag).Language;
}
else if (SeleNode.ImageIndex == 1 || SeleNode.ImageIndex == 2)
{
ParLang = ((treeTagNote)SeleNode.Tag).Language;
}
}
ProperDialog propDia = new ProperDialog("0", "", ParLang);
DialogResult dr = propDia.ShowDialog();
if (dr == DialogResult.OK)
{
string Title = propDia.ReturnVal[0];
string Language = propDia.ReturnVal[1];
string IsOnRoot = propDia.ReturnVal[2];
string sGUID = System.Guid.NewGuid().ToString();
//新建根级目录
if (IsOnRoot == "True" || isCreateRoot == "True")
{
//插入树节点
TreeNode InsertNodeDir = new TreeNode(Title);
InsertNodeDir.Name = sGUID;
InsertNodeDir.ImageIndex = 0;
InsertNodeDir.SelectedImageIndex = 0;
treeTagBook tb = new treeTagBook();
tb.Language = Language;
InsertNodeDir.Tag = tb;
treeViewYouDao.Nodes.Insert(treeViewYouDao.Nodes.Count, InsertNodeDir);
treeViewYouDao.SelectedNode = InsertNodeDir;
//更新本地XML文档
XmlDocument xDoc = new XmlDocument();
xDoc.Load("TreeNodeLocal.xml");
XmlNode xseleNode = xDoc.DocumentElement;
XmlElement appEle = xDoc.CreateElement("book");
appEle.SetAttribute("id", sGUID);
appEle.SetAttribute("title", Title);
appEle.SetAttribute("Language", Language);
xseleNode.AppendChild(appEle);
xDoc.Save("TreeNodeLocal.xml");
//同步到云端
XMLAPI.XML2Yun();
////------同步到本地缓存数据库
OleDbConnection ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
string NewPid = "0";
string NewNodeId = "1";
string NewTurn = "1";
string SynId = PubFunc.Language2Synid(Language);
if (IsOnRoot == "True")
{
NewNodeId = AccessAdo.ExecuteScalar(ExportConn, "select max(NodeId) from ttree").ToString();
NewNodeId = NewNodeId == "" ? "1" : (Convert.ToInt32(NewNodeId) + 1).ToString();
NewTurn = AccessAdo.ExecuteScalar(ExportConn, "select max(Turn) from ttree where parentId=" + NewPid).ToString();
NewTurn = NewTurn == "" ? "1" : (Convert.ToInt32(NewTurn) + 1).ToString();
}
//插入数据库记录
DateTime d1 = DateTime.Parse("1970-01-01 08:00:00");
DateTime d2 = DateTime.Now;
TimeSpan dt = d2 - d1;
//相差秒数
string Seconds = dt.TotalSeconds.ToString();
//插入TTREE
string sql = string.Format("insert into ttree(NodeID,Title,ParentId,Type,CreateTime,SynId,Turn,MarkTime,IsLock,Gid) values({0},'{1}',{2},{3},{4},{5},{6},{7},{8},'{9}')", NewNodeId, Title, NewPid, 0, Seconds, SynId, NewTurn,0,0,sGUID);
AccessAdo.ExecuteNonQuery(ExportConn, sql);
}
else if (SeleNode != null)
{
if ((SeleNode.ImageIndex == 1||SeleNode.ImageIndex == 2) && IsOnRoot == "False")
{
MessageBox.Show("不能在文章下新增节点!");
return;
}
//插入树节点
TreeNode InsertNodeDir = new TreeNode(Title);
InsertNodeDir.Name = sGUID;
InsertNodeDir.ImageIndex = 0;
InsertNodeDir.SelectedImageIndex = 0;
treeTagBook tb = new treeTagBook();
tb.Language = Language;
InsertNodeDir.Tag = tb;
SeleNode.Nodes.Insert(SeleNode.Nodes.Count, InsertNodeDir);
treeViewYouDao.SelectedNode = InsertNodeDir;
//更新本地XML文档
XmlDocument xDoc = new XmlDocument();
xDoc.Load("TreeNodeLocal.xml");
XmlNode xseleNode = xDoc.SelectSingleNode("//book[@id='" + SeleNode.Name + "']");
XmlElement appEle = xDoc.CreateElement("book");
appEle.SetAttribute("id", sGUID);
appEle.SetAttribute("title", Title);
appEle.SetAttribute("Language", Language);
xseleNode.AppendChild(appEle);
xDoc.Save("TreeNodeLocal.xml");
//同步到云端
XMLAPI.XML2Yun();
////------同步到本地缓存数据库
OleDbConnection ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
string NewPid = AccessAdo.ExecuteScalar(ExportConn, "select nodeid from ttree where gid='" + SeleNode.Name + "'").ToString();
string NewNodeId = AccessAdo.ExecuteScalar(ExportConn,"select max(NodeId) from ttree").ToString();
NewNodeId = NewNodeId == "" ? "1" : (Convert.ToInt32(NewNodeId) + 1).ToString();
string NewTurn = AccessAdo.ExecuteScalar(ExportConn,"select max(Turn) from ttree where parentId=" + NewPid).ToString();
NewTurn = NewTurn == "" ? "1" : (Convert.ToInt32(NewTurn) + 1).ToString();
string SynId = PubFunc.Language2Synid(Language);
//顶层
if (IsOnRoot == "True")
{
NewPid = "0";
NewTurn = AccessAdo.ExecuteScalar(ExportConn,"select max(Turn) from ttree where parentId=0").ToString();
NewTurn = NewTurn == "" ? "1" : (Convert.ToInt32(NewTurn) + 1).ToString();
}
//插入数据库记录
DateTime d1 = DateTime.Parse("1970-01-01 08:00:00");
DateTime d2 = DateTime.Now;
TimeSpan dt = d2 - d1;
//相差秒数
string Seconds = dt.TotalSeconds.ToString();
//插入TTREE
string sql = string.Format("insert into ttree(NodeID,Title,ParentId,Type,CreateTime,SynId,Turn,MarkTime,IsLock,Gid) values({0},'{1}',{2},{3},{4},{5},{6},{7},{8},'{9}')", NewNodeId, Title, NewPid, 0, Seconds, SynId, NewTurn, 0, 0, sGUID);
AccessAdo.ExecuteNonQuery(ExportConn,sql);
}
}
}
//右键菜单
private void treeViewYouDao_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)//判断你点的是不是右键
{
Point ClickPoint = new Point(e.X, e.Y);
TreeNode CurrentNode = treeViewYouDao.GetNodeAt(ClickPoint);
if (CurrentNode != null)//判断你点的是不是一个节点
{
switch (CurrentNode.SelectedImageIndex.ToString())//根据不同节点显示不同的右键菜单
{
case "0"://目录
CurrentNode.ContextMenuStrip = contextMenuStripYDdir;
break;
case "1":
CurrentNode.ContextMenuStrip = contextMenuStripYDtxt;
toolStripMenuItemEncrypt.Visible = true;
toolStripMenuItemDecrypt.Visible = false;
break;
case "2":
CurrentNode.ContextMenuStrip = contextMenuStripYDtxt;
toolStripMenuItemEncrypt.Visible = false;
toolStripMenuItemDecrypt.Visible = true;
break;
default:
break;
}
treeViewYouDao.SelectedNode = CurrentNode;//选中这个节点
}
else
{
//右击空白区域
treeViewYouDao.ContextMenuStrip = contextMenuStripYDblank;
}
}
}
//新建文章
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
//获取选中节点
TreeNode SeleNode = treeViewYouDao.SelectedNode;
string isHaveNodes = "True";
string ParLang = "TEXT";
//如果没有选中节点,则新建顶层目录
if (SeleNode == null || treeViewYouDao.Nodes.Count == 0)
{
isHaveNodes = "False";
}
else
{
if (SeleNode.ImageIndex == 0)
{
ParLang = ((treeTagBook)SeleNode.Tag).Language;
}
else if (SeleNode.ImageIndex == 1 || SeleNode.ImageIndex == 2)
{
ParLang = ((treeTagNote)SeleNode.Tag).Language;
}
}
ProperDialog propDia = new ProperDialog("1", "", ParLang);
DialogResult dr = propDia.ShowDialog();
if (dr == DialogResult.OK)
{
string Title = propDia.ReturnVal[0];
string Language = propDia.ReturnVal[1];
string IsOnRoot = propDia.ReturnVal[2];
string SynId = PubFunc.Language2Synid(Language);
string sGUID = System.Guid.NewGuid().ToString();
//云端创建笔记,返回路径
YouDaoNode2 node = NoteAPI.CreateNote(Title);
string Path = node.GetPath();
//无节点,直接顶层创建
if (isHaveNodes == "False")
{
//插入树节点
TreeNode InsertNodeNote = new TreeNode(Title);
InsertNodeNote.Name = sGUID;
InsertNodeNote.ImageIndex = 1;
InsertNodeNote.SelectedImageIndex = 1;
treeTagNote tag = new treeTagNote();
tag.path = Path;
tag.createtime = PubFunc.time2TotalSeconds().ToString();
tag.Language = Language;
tag.isMark = "0";
InsertNodeNote.Tag = tag;
treeViewYouDao.Nodes.Insert(treeViewYouDao.Nodes.Count, InsertNodeNote);
treeViewYouDao.SelectedNode = InsertNodeNote;
//更新本地XML文档
XmlDocument xDoc = new XmlDocument();
xDoc.Load("TreeNodeLocal.xml");
XmlNode xseleNode = xDoc.DocumentElement;
XmlElement appEle = xDoc.CreateElement("note");
appEle.SetAttribute("id", sGUID);
appEle.SetAttribute("title", Title);
appEle.SetAttribute("path", Path);
appEle.SetAttribute("createtime", tag.createtime);
appEle.SetAttribute("Language", Language);
appEle.SetAttribute("isMark", "0");
appEle.SetAttribute("IsLock", "0");
xseleNode.AppendChild(appEle);
xDoc.Save("TreeNodeLocal.xml");
//同步到云端
XMLAPI.XML2Yun();
//---------同步到缓存数据库
OleDbConnection ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
string NewPid = "0";
string NewNodeId = "1";
string NewTurn = "1";
//插入数据库记录
DateTime d1 = DateTime.Parse("1970-01-01 08:00:00");
DateTime d2 = DateTime.Now;
TimeSpan dt = d2 - d1;
//相差秒数
string Seconds = dt.TotalSeconds.ToString();
//插入TTREE
string sql = string.Format("insert into ttree(NodeID,Title,Path,ParentId,Type,CreateTime,SynId,Turn,MarkTime,IsLock,Gid) values({0},'{1}','{2}',{3},{4},{5},{6},{7},{8},{9},'{10}')", NewNodeId, Title,Path, NewPid, 1, Seconds, SynId, NewTurn,0,0,sGUID);
AccessAdo.ExecuteNonQuery(ExportConn,sql);
//插入TTcontent
sql = string.Format("insert into tcontent(NodeId,updatetime,Gid,Path,NeedSync) values({0},{1},'{2}','{3}',{4})", NewNodeId, Seconds,sGUID,Path,0);
AccessAdo.ExecuteNonQuery(ExportConn,sql);
//新窗口打开编辑界面
string lastTime="最后更新时间:"+DateTime.Now.ToString();
formParent.openNewYouDao(Path, Title, treeViewYouDao.SelectedNode.FullPath,1);
//打开后设置语言
Language = PubFunc.Synid2LanguageSetLang(SynId);
formParent.SetLanguage(Language);
}
else if (SeleNode != null)
{
if (Path != "")
{
//顶层创建文章
if (IsOnRoot == "True")
{
//插入树节点
TreeNode InsertNodeNote = new TreeNode(Title);
InsertNodeNote.Name = sGUID;
InsertNodeNote.ImageIndex = 1;
InsertNodeNote.SelectedImageIndex = 1;
treeTagNote tag = new treeTagNote();
tag.path = Path;
tag.createtime = PubFunc.time2TotalSeconds().ToString();
tag.Language = Language;
tag.isMark = "0";
InsertNodeNote.Tag = tag;
treeViewYouDao.Nodes.Insert(treeViewYouDao.Nodes.Count, InsertNodeNote);
treeViewYouDao.SelectedNode = InsertNodeNote;
//更新本地XML文档
XmlDocument xDoc = new XmlDocument();
xDoc.Load("TreeNodeLocal.xml");
XmlNode xseleNode = xDoc.DocumentElement;
XmlElement appEle = xDoc.CreateElement("note");
appEle.SetAttribute("id", sGUID);
appEle.SetAttribute("title", Title);
appEle.SetAttribute("path", Path);
appEle.SetAttribute("createtime", tag.createtime);
appEle.SetAttribute("Language", Language);
appEle.SetAttribute("isMark", "0");
appEle.SetAttribute("IsLock", "0");
xseleNode.AppendChild(appEle);
xDoc.Save("TreeNodeLocal.xml");
//同步到云端
XMLAPI.XML2Yun();
////-------------同步到缓存数据库
OleDbConnection ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
string NewPid = AccessAdo.ExecuteScalar(ExportConn, "select nodeid from ttree where gid='" + SeleNode.Name + "'").ToString();
string NewNodeId = AccessAdo.ExecuteScalar(ExportConn,"select max(NodeId) from ttree").ToString();
NewNodeId = NewNodeId == "" ? "1" : (Convert.ToInt32(NewNodeId) + 1).ToString();
string NewTurn = AccessAdo.ExecuteScalar(ExportConn,"select max(Turn) from ttree where parentId=" + NewPid).ToString();
NewTurn = NewTurn == "" ? "1" : (Convert.ToInt32(NewTurn) + 1).ToString();
//顶层
if (IsOnRoot == "True")
{
NewPid = "0";
NewTurn = AccessAdo.ExecuteScalar(ExportConn,"select max(Turn) from ttree where parentId=0").ToString();
NewTurn = NewTurn == "" ? "1" : (Convert.ToInt32(NewTurn) + 1).ToString();
}
//插入数据库记录
DateTime d1 = DateTime.Parse("1970-01-01 08:00:00");
DateTime d2 = DateTime.Now;
TimeSpan dt = d2 - d1;
//相差秒数
string Seconds = dt.TotalSeconds.ToString();
//插入TTREE
string sql = string.Format("insert into ttree(NodeID,Title,path,ParentId,Type,CreateTime,SynId,Turn,MarkTime,IsLock,Gid) values({0},'{1}','{2}',{3},{4},{5},{6},{7},{8},{9},'{10}')", NewNodeId, Title,Path, NewPid, 1, Seconds, SynId, NewTurn,0,0,sGUID);
AccessAdo.ExecuteNonQuery(ExportConn,sql);
//插入TTcontent
sql = string.Format("insert into tcontent(NodeId,updatetime,Gid,Path,NeedSync) values({0},{1},'{2}','{3}',{4})", NewNodeId, Seconds, sGUID, Path, 0);
AccessAdo.ExecuteNonQuery(ExportConn,sql);
//新窗口打开编辑界面
string lastTime = "最后更新时间:" + DateTime.Now.ToString();
formParent.openNewYouDao(Path, Title, treeViewYouDao.SelectedNode.FullPath,1);
//打开后设置语言
Language = PubFunc.Synid2LanguageSetLang(SynId);
formParent.SetLanguage(Language);
}
else
{
if ((SeleNode.ImageIndex == 1||SeleNode.ImageIndex == 2) && IsOnRoot == "False")
{
MessageBox.Show("不能在文章下新增节点!");
return;
}
//插入树节点
TreeNode InsertNodeNote = new TreeNode(Title);
treeTagNote tag = new treeTagNote();
tag.path = Path;
tag.createtime = PubFunc.time2TotalSeconds().ToString();
tag.Language = Language;
tag.isMark = "0";
InsertNodeNote.Name = sGUID;
InsertNodeNote.ImageIndex = 1;
InsertNodeNote.SelectedImageIndex = 1;
InsertNodeNote.Tag = tag;
SeleNode.Nodes.Insert(SeleNode.Nodes.Count, InsertNodeNote);
treeViewYouDao.SelectedNode = InsertNodeNote;
//更新本地XML文档
XmlDocument xDoc = new XmlDocument();
xDoc.Load("TreeNodeLocal.xml");
XmlNode xseleNode = xDoc.SelectSingleNode("//book[@id='" + SeleNode.Name + "']");
XmlElement appEle = xDoc.CreateElement("note");
appEle.SetAttribute("id", sGUID);
appEle.SetAttribute("title", Title);
appEle.SetAttribute("path", Path);
appEle.SetAttribute("createtime", tag.createtime);
appEle.SetAttribute("Language", Language);
appEle.SetAttribute("isMark", "0");
appEle.SetAttribute("IsLock", "0");
xseleNode.AppendChild(appEle);
xDoc.Save("TreeNodeLocal.xml");
//同步到云端
XMLAPI.XML2Yun();
////-------------同步到缓存数据库
OleDbConnection ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
string NewPid = AccessAdo.ExecuteScalar(ExportConn, "select nodeid from ttree where gid='" + SeleNode.Name + "'").ToString();
string NewNodeId = AccessAdo.ExecuteScalar(ExportConn, "select max(NodeId) from ttree").ToString();
NewNodeId = NewNodeId == "" ? "1" : (Convert.ToInt32(NewNodeId) + 1).ToString();
string NewTurn = AccessAdo.ExecuteScalar(ExportConn, "select max(Turn) from ttree where parentId=" + NewPid).ToString();
NewTurn = NewTurn == "" ? "1" : (Convert.ToInt32(NewTurn) + 1).ToString();
//顶层
if (IsOnRoot == "True")
{
NewPid = "0";
NewTurn = AccessAdo.ExecuteScalar(ExportConn, "select max(Turn) from ttree where parentId=0").ToString();
NewTurn = NewTurn == "" ? "1" : (Convert.ToInt32(NewTurn) + 1).ToString();
}
//插入数据库记录
DateTime d1 = DateTime.Parse("1970-01-01 08:00:00");
DateTime d2 = DateTime.Now;
TimeSpan dt = d2 - d1;
//相差秒数
string Seconds = dt.TotalSeconds.ToString();
//插入TTREE
string sql = string.Format("insert into ttree(NodeID,Title,Path,ParentId,Type,CreateTime,SynId,Turn,MarkTime,IsLock,Gid) values({0},'{1}','{2}',{3},{4},{5},{6},{7},{8},{9},'{10}')", NewNodeId, Title,Path, NewPid, 1, Seconds, SynId, NewTurn, 0, 0, sGUID);
AccessAdo.ExecuteNonQuery(ExportConn, sql);
//插入TTcontent
sql = string.Format("insert into tcontent(NodeId,updatetime,Gid,Path,NeedSync) values({0},{1},'{2}','{3}',{4})", NewNodeId, Seconds, sGUID, Path, 0);
AccessAdo.ExecuteNonQuery(ExportConn, sql);
//新窗口打开编辑界面
string lastTime = "最后更新时间:" + DateTime.Now.ToString();
formParent.openNewYouDao(Path, Title, treeViewYouDao.SelectedNode.FullPath,1);
//打开后设置语言
Language = PubFunc.Synid2LanguageSetLang(SynId);
formParent.SetLanguage(Language);
}
}
}
}
}
//删除目录或者文章
private void toolStripMenuItem3_Click(object sender, EventArgs e)
{
//避免保存的提示
Attachment.isDeleteClose = "1";
//获取选中节点
TreeNode SeleNode = treeViewYouDao.SelectedNode;
if (SeleNode == null)
return;
//删除前确认
if (MessageBox.Show("当前节点及其所有子节点都会被删除,继续?", "提示", MessageBoxButtons.YesNo) == DialogResult.No)
{
return;
}
//先查找到所选节点下面所有的文章进行删除
//删除云数据
DelNodeData(SeleNode.Name);
//更新缓存数据
OleDbConnection ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
DelLocalData(AccessAdo.ExecuteScalar(ExportConn,"select nodeid from ttree where gid='"+SeleNode.Name+"'").ToString());
//移除树节点
treeViewYouDao.Nodes.Remove(SeleNode);
Attachment.isDeleteClose = "0";
formParent.ReSetMarkFind();
}
//删除有道云数据,同时关闭已打开的文章,再更新本地XML并同步到云
public void DelNodeData(string id)
{
XmlDocument doc = new XmlDocument();
doc.Load("TreeNodeLocal.xml");
XmlNode seleNode = doc.SelectSingleNode("//node()[@id='" + id + "']");
if (seleNode.Name == "note")
{
//选中的是文章
string path = seleNode.Attributes["path"].Value;
//关闭打开的文章
formParent.CloseDoc(path);
NoteAPI.DeleteNote(path);
}
else
{
XmlNodeList xlist = seleNode.SelectNodes("//node()[@id='" + id + "']//note");
foreach (XmlNode xnode in xlist)
{
string path = xnode.Attributes["path"].Value;
//删除笔记
//关闭打开的文章
formParent.CloseDoc(path);
NoteAPI.DeleteNote(path);
}
}
//移除XML节点,更新XML到云
seleNode.ParentNode.RemoveChild(seleNode);
doc.Save("TreeNodeLocal.xml");
XMLAPI.XML2Yun();
}
//删除本地缓存数据
public void DelLocalData(string NodeId)
{
OleDbConnection ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
string SQL = string.Format("select NodeId from Ttree where parentId={0}", NodeId);
DataTable temp = AccessAdo.ExecuteDataSet(ExportConn,SQL).Tables[0];
DataView dv = new DataView(temp);
foreach (DataRowView drv in dv)
{
DelLocalData(drv["NodeId"].ToString());
}
ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
string DelSQL = string.Format("Delete from TAttachment where NodeId={0}", NodeId);
AccessAdo.ExecuteNonQuery(ExportConn,DelSQL);
DelSQL = string.Format("Delete from Tcontent where NodeId={0}", NodeId);
AccessAdo.ExecuteNonQuery(ExportConn,DelSQL);
DelSQL = string.Format("Delete from Ttree where NodeId={0}", NodeId);
AccessAdo.ExecuteNonQuery(ExportConn,DelSQL);
}
private void toolStripMenuItem6_Click(object sender, EventArgs e)
{
toolStripMenuItem3_Click(sender, e);
}
#region 上下移动操作
//上移
public void setNodeUp()
{
//树操作
SetTreeNodeUp(this.treeViewYouDao.SelectedNode);
}
private void SetTreeNodeUp(System.Windows.Forms.TreeNode node)
{
if ((node == null) || (node.PrevNode) == null) return;
System.Windows.Forms.TreeNode newNode = (System.Windows.Forms.TreeNode)node.Clone();
//要交换次序的节点
string NodeId1 = node.Name.ToString();
string NodeId2 = node.PrevNode.Name.ToString();
if (node.Parent != null)
node.Parent.Nodes.Insert(node.PrevNode.Index, newNode);
else
node.TreeView.Nodes.Insert(node.PrevNode.Index, newNode);
this.treeViewYouDao.Nodes.Remove(node);
this.treeViewYouDao.SelectedNode = newNode;
treeViewYouDao.Focus();
//xml移动
xmlNodeMove(NodeId2, NodeId1);
//xml同步
XMLAPI.XML2Yun();
}
//下移
public void setNodeDown()
{
//树操作
SetTreeNodeDown(this.treeViewYouDao.SelectedNode);
}
private void SetTreeNodeDown(System.Windows.Forms.TreeNode node)
{
if ((node == null) || (node.NextNode) == null) return;
System.Windows.Forms.TreeNode newNode = (System.Windows.Forms.TreeNode)node.Clone();
//要交换次序的节点
string NodeId1 = node.Name.ToString();
string NodeId2 = node.NextNode.Name.ToString();
if (node.Parent != null)
node.Parent.Nodes.Insert(node.NextNode.Index + 1, newNode);
else
node.TreeView.Nodes.Insert(node.NextNode.Index + 1, newNode);
this.treeViewYouDao.Nodes.Remove(node);
this.treeViewYouDao.SelectedNode = newNode;
treeViewYouDao.Focus();
//xml移动
xmlNodeMove(NodeId1, NodeId2);
//xml同步
XMLAPI.XML2Yun();
}
/// <summary>
/// 交换xml节点的顺序
/// </summary>
/// <param name="id1">前一个节点id</param>
/// <param name="id2">后一个节点id</param>
private void xmlNodeMove(string id1, string id2)
{
XmlDocument doc = new XmlDocument();
doc.Load("TreeNodeLocal.xml");
XmlNode preNode = doc.SelectSingleNode("//node()[@id='" + id1 + "']");
XmlNode parentNode = preNode.ParentNode;
XmlNode nexNode = doc.SelectSingleNode("//node()[@id='" + id2 + "']");
parentNode.InsertAfter(preNode, nexNode);
doc.Save("TreeNodeLocal.xml");
}
#endregion
private void toolStripMenuItem8_Click(object sender, EventArgs e)
{
toolStripMenuItem1_Click(sender, e);
}
//重命名以及语言
private void toolStripMenuItem4_Click(object sender, EventArgs e)
{
//获取选中节点
TreeNode SeleNode = this.treeViewYouDao.SelectedNode;
if (SeleNode == null)
return;
string ParLang, Type, DiaType;
if (SeleNode.ImageIndex == 0)
{
//目录
ParLang = ((treeTagBook)SeleNode.Tag).Language;
DiaType = "2";