-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathFormMain.cs
2157 lines (1855 loc) · 73.5 KB
/
FormMain.cs
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.Data.OleDb;
using System.IO;
using System.Configuration;
using ScintillaNET;
using WeifenLuo.WinFormsUI.Docking;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Web;
using System.Xml;
using System.Net;
using System.Threading;
namespace WeCode1._0
{
public partial class FormMain : Form
{
private FormTreeLeft frTree;
private YouDaoTree frYoudaoTree;
private FormAttachment frmAttchment;
private DocMark frmMark;
private DocFind FormFind;
private DeserializeDockContent m_deserializeDockContent;
#region Fields
private int _newDocumentCount = 0;
private string[] _args;
private int _zoomLevel=0;
private const int LINE_NUMBERS_MARGIN_WIDTH = 50;
#endregion Fields
#region Properties
public DocumentForm ActiveDocument
{
get
{
return dockPanel1.ActiveDocument as DocumentForm;
}
}
#endregion Properties
// 当收到第二个进程的通知时,显示窗体
private void OnProgramStarted(object state, bool timeout)
{
toolStripMenuItem2_Click(null,null);
}
public FormMain()
{
ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
InitializeComponent();
//版本检查
//DateTime d1 = DateTime.Now;
//CheckVer();
//DateTime d2 = DateTime.Now;
//TimeSpan sp = d2 - d1;
//MessageBox.Show(sp.TotalMilliseconds.ToString());
//书签
frmMark = new DocMark();
frmMark.formParent = this;
//frmMark.Show(dockPanel1);
//显示查找
FormFind = new DocFind();
FormFind.formParent = this;
//FormFind.Show(dockPanel1);
//显示树窗口
frTree = new FormTreeLeft();
frTree.formParent = this;
//frTree.Show(dockPanel1);
//显示有道树窗口
frYoudaoTree = new YouDaoTree();
frYoudaoTree.formParent = this;
//frYoudaoTree.Show(dockPanel1);
//显示附件窗口
Attachment.ActiveNodeId = "-1";
frmAttchment = new FormAttachment();
Attachment.AttForm = frmAttchment;
//frmAttchment.Show(dockPanel1);
m_deserializeDockContent = new DeserializeDockContent(GetContentFromPersistString);
IniPanel();
//第一次打开界面,先判断token是否有效(为空或者过期)
//有效,同步XML到本地,加载有道云树目录;无效,打开授权页面进行授权
//授权成功后,云端创建两个目录以及配置文件,然后加载树目录
//IniYouDaoAuthor();
//-------移至有道窗口多线程校验-----------//
//上报统计信息
Thread t = new Thread(new ThreadStart(UpUerInfo));
t.Start();
}
public void UpUerInfo()
{
try
{
string uuid = PubFunc.GetConfiguration("UUID");
string ver = PubFunc.GetConfiguration("Version");
string Token = PubFunc.GetConfiguration("AccessToken");
string url = "http://wecode.thinkry.com/c/ping?u=" + uuid + "&v=" + ver + "&yd=" + Token;
WebClient MyWebClient = new WebClient();
MyWebClient.Credentials = CredentialCache.DefaultCredentials;//获取或设置用于对向Internet资源的请求进行身份验证的网络凭据。
Byte[] pageData = MyWebClient.DownloadData(url); //从指定网站下载数据
}
catch (WebException webEx)
{
Console.WriteLine(webEx.Message.ToString());
}
}
//初始化相关
private void IniPanel()
{
try
{
Attachment.frmMain = this;
//加载布局
string configFile = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "DockPanel.config");
if (File.Exists(configFile))
dockPanel1.LoadFromXml(configFile, m_deserializeDockContent);
//打开欢迎界面
openWelcomePage();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void setSkin()
{
this.dockPanel1.Skin.DockPaneStripSkin.DocumentGradient.DockStripGradient.StartColor = Color.FromArgb(228, 226, 213);
this.dockPanel1.Skin.DockPaneStripSkin.DocumentGradient.DockStripGradient.EndColor = Color.FromArgb(228, 226, 213);
this.dockPanel1.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.StartColor = Color.FromArgb(204, 199, 186);
this.dockPanel1.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.EndColor = Color.FromArgb(204, 199, 186);
//this.dockPanel1.Skin.DockPaneStripSkin.DocumentGradient.InactiveTabGradient.StartColor = Color.FromArgb(204, 199, 186);
//this.dockPanel1.Skin.DockPaneStripSkin.DocumentGradient.InactiveTabGradient.EndColor = Color.FromArgb(204, 199, 186);
}
//初始化授权相关
public void IniYouDaoAuthor()
{
//判断token是否有效
string IsAuthor = AuthorAPI.GetIsAuthor();
if (IsAuthor != "OK")
{
//禁用云目录
this.toolStripMenuItemLogin.Visible = true;
this.toolStripMenuItemUinfo.Visible = false;
Attachment.IsTokeneffective = 0;
this.Load += new System.EventHandler(this.showNoAuthor);
}
else
{
//从云端拉取XML同步到本地
XMLAPI.Yun2XML();
Attachment.IsTokeneffective = 1;
//获取用户信息并禁用登陆按钮
this.toolStripMenuItemLogin.Visible = false;
this.toolStripMenuItemUinfo.Visible = true;
}
}
private void showNoAuthor(object sender, System.EventArgs e)
{
//MessageBox.Show("未授权有道云笔记或者授权已过期,请点击用户--登录以重新授权!");
if (ConfigurationManager.AppSettings["authorAlert"] != "0")
{
if (MessageBox.Show("未授权有道云笔记或者授权已过期,点击菜单用户--登录以重新授权!\n\n点击“确定”不再提醒", "登录提醒", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
{
PubFunc.SetConfiguration("authorAlert", "0");
}
}
}
//上移
private void toolStripButtonUp_Click(object sender, EventArgs e)
{
if(dockPanel1.ActiveContent.GetType()==typeof(FormTreeLeft))
{
frTree.setNodeUp();
}
else if (dockPanel1.ActiveContent.GetType() == typeof(YouDaoTree))
{
frYoudaoTree.setNodeUp();
}
else
{
return;
}
}
//下移
private void toolStripButtonDown_Click(object sender, EventArgs e)
{
if (dockPanel1.ActiveContent.GetType() == typeof(FormTreeLeft))
{
frTree.setNodeDown();
}
else if (dockPanel1.ActiveContent.GetType() == typeof(YouDaoTree))
{
frYoudaoTree.setNodeDown();
}
else
{
return;
}
}
//关闭文章
public void CloseDoc(string nodeId)
{
if (Attachment.isWelcomePageopen == "0")
{
foreach (DocumentForm documentForm in dockPanel1.Documents)
{
if (nodeId.Equals(documentForm.NodeId, StringComparison.OrdinalIgnoreCase))
{
documentForm.Close();
break;
}
}
}
}
//将指定ID的文章标示为加密
public void SetLock(string nodeId)
{
if (Attachment.isWelcomePageopen == "0")
{
foreach (DocumentForm documentForm in dockPanel1.Documents)
{
if (nodeId.Equals(documentForm.NodeId, StringComparison.OrdinalIgnoreCase))
{
documentForm.IsLock = 1;
break;
}
}
}
}
//将指定ID的文章标示为未加密
public void UnsetLock(string nodeId)
{
if (Attachment.isWelcomePageopen == "0")
{
foreach (DocumentForm documentForm in dockPanel1.Documents)
{
if (nodeId.Equals(documentForm.NodeId, StringComparison.OrdinalIgnoreCase))
{
documentForm.IsLock = 0;
break;
}
}
}
}
//打开文章
public void openNew(string nodeId,string treeLocation,string updateTime,int imageType)
{
//欢迎窗口是否打开,如果打开则关闭
if (Attachment.isWelcomePageopen == "1")
{
IDockContent[] documents = dockPanel1.DocumentsToArray();
foreach (IDockContent content in documents)
{
content.DockHandler.Close();
}
Attachment.isWelcomePageopen = "0";
}
// 如果已经打开,则定位,否则新窗口打开
bool isOpen = false;
foreach (DocumentForm documentForm in dockPanel1.Documents)
{
if (nodeId.Equals(documentForm.NodeId, StringComparison.OrdinalIgnoreCase))
{
documentForm.Select();
Attachment.isnewOpenDoc = "0";
isOpen = true;
break;
}
}
// Open the files
if (!isOpen)
OpenFile(nodeId,treeLocation,updateTime,imageType);
}
//打开云笔记
public void openNewYouDao(string nodeId,string title,string treeLocation,int imageType)
{
//欢迎窗口是否打开,如果打开则关闭
if (Attachment.isWelcomePageopen == "1")
{
IDockContent[] documents = dockPanel1.DocumentsToArray();
foreach (IDockContent content in documents)
{
content.DockHandler.Close();
}
Attachment.isWelcomePageopen = "0";
}
// 如果已经打开,则定位,否则新窗口打开
bool isOpen = false;
foreach (DocumentForm documentForm in dockPanel1.Documents)
{
if (nodeId.Equals(documentForm.NodeId, StringComparison.OrdinalIgnoreCase))
{
documentForm.Select();
Attachment.isnewOpenDoc = "0";
isOpen = true;
break;
}
}
// Open the files
if (!isOpen)
OpenFileYouDao(nodeId,title,treeLocation,imageType);
}
private DocumentForm OpenFileYouDao(string nodeId, string title, string treeLocation,int imageType)
{
Attachment.isnewOpenDoc = "1";
string Content;
string updatetime;
//获取文章信息
//如果缓存还没更新则提示是从缓存读取还是直接读取有道云
OleDbConnection ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
string SQL = "select Content,UpdateTime from tcontent where path='" + nodeId + "' and needsync=1";
DataTable dt=AccessAdo.ExecuteDataSet(ExportConn,SQL).Tables[0];
youdao.YouDaoNode2 note = null;
do
{
if (dt.Rows.Count > 0)
{
//还未同步
if (MessageBox.Show("该笔记尚未完成同步,点击确定打开本地文章,取消打开云端文章", "选择信息!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
//打开本地文章
Content = dt.Rows[0]["Content"].ToString();
updatetime = dt.Rows[0]["UpdateTime"].ToString();
break;
}
}
note = NoteAPI.GetNote(nodeId);
if (note == null)
{
MessageBox.Show("从有道云打开笔记失败...");
return null;
}
Content = note.GetContent();
updatetime = "最后更新时间:" + (PubFunc.seconds2Time(Convert.ToInt32(note.GetUpdateTime()))).ToString();
//写入缓存
ExportConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+PubFunc.GetYoudaoDBPath());
OleDbParameter p1 = new OleDbParameter("@Content", OleDbType.VarChar);
p1.Value = Content;
OleDbParameter p2 = new OleDbParameter("@path", OleDbType.VarChar);
p2.Value = nodeId;
OleDbParameter[] ArrPara = new OleDbParameter[2];
ArrPara[0] = p1;
ArrPara[1] = p2;
SQL = "update tcontent set content=@Content,needSync=0 where path=@path";
AccessAdo.ExecuteNonQuery(ExportConn, SQL, ArrPara);
} while(false);
if (imageType == 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 null;
else
{
Content = EncryptDecrptt.DecrptyByKey(Content, MykeydYd);
}
}
DocumentForm doc = new DocumentForm();
doc.TreeLocation = treeLocation;
doc.LastUpdateTime = updatetime;
doc.IsLock = imageType - 1;
SetScintillaToCurrentOptions(doc);
doc.Scintilla.Text = Content;
doc.Scintilla.UndoRedo.EmptyUndoBuffer();
doc.Scintilla.Modified = false;
doc.Text = title;
doc.NodeId = nodeId;
//doc.Data = note;
doc.Type = "online";
doc.Show(dockPanel1);
return doc;
}
////打开文章
//public void openNew(string nodeId)
//{
// // 如果已经打开,则定位,否则新窗口打开
// bool isOpen = false;
// foreach (DocumentForm documentForm in dockPanel1.Documents)
// {
// if (nodeId.Equals(documentForm.NodeId, StringComparison.OrdinalIgnoreCase))
// {
// documentForm.Select();
// isOpen = true;
// break;
// }
// }
// // Open the files
// if (!isOpen)
// OpenFile(nodeId);
//}
private DocumentForm OpenFile(string nodeId,string treeLocation,string updateTime,int imageType)
{
Attachment.isnewOpenDoc = "1";
//获取文章信息
string SQL = "select Title,Content from TContent inner join TTree on TContent.NodeId=Ttree.NodeId where TContent.NodeId=" + nodeId;
DataTable temp = AccessAdo.ExecuteDataSet(SQL, null).Tables[0];
if (temp.Rows.Count == 0)
return null;
string Title = temp.Rows[0]["Title"].ToString();
string Content = temp.Rows[0]["Content"].ToString();
if (imageType == 2)
{
//加密,对content解密
string Mykeyd = "";
if (Attachment.KeyD != "")
{
//内存中已存在秘钥
Mykeyd = Attachment.KeyD;
}
else
{
//内存中不存在秘钥
DialogPSW dp = new DialogPSW("3");
DialogResult dr = dp.ShowDialog();
if (dr == DialogResult.OK)
{
Mykeyd = dp.ReturnVal;
}
}
if (Mykeyd == "")
return null;
else
{
Content = EncryptDecrptt.DecrptyByKey(Content, Mykeyd);
}
}
DocumentForm doc = new DocumentForm();
doc.TreeLocation = treeLocation;
doc.LastUpdateTime = updateTime;
doc.IsLock = imageType - 1;
SetScintillaToCurrentOptions(doc);
doc.Scintilla.Text = Content;
doc.Scintilla.UndoRedo.EmptyUndoBuffer();
doc.Scintilla.Modified = false;
doc.Text = Title;
doc.NodeId = nodeId;
doc.Type = "local";
doc.Show(dockPanel1);
return doc;
}
//配置相关显示参数
private void SetScintillaToCurrentOptions(DocumentForm doc)
{
//// Turn on line numbers?
if (toolStripMenuItemLn.Checked)
doc.Scintilla.Margins.Margin0.Width = LINE_NUMBERS_MARGIN_WIDTH;
else
doc.Scintilla.Margins.Margin0.Width = 0;
//// Turn on white space?
//if (whitespaceToolStripMenuItem.Checked)
// doc.Scintilla.Whitespace.Mode = WhitespaceMode.VisibleAlways;
//else
// doc.Scintilla.Whitespace.Mode = WhitespaceMode.Invisible;
//// Turn on word wrap?
//if (wordWrapToolStripMenuItem.Checked)
// doc.Scintilla.LineWrapping.Mode = LineWrappingMode.Word;
//else
// doc.Scintilla.LineWrapping.Mode = LineWrappingMode.None;
//// Show EOL?
//doc.Scintilla.EndOfLine.IsVisible = endOfLineToolStripMenuItem.Checked;
// Set the zoom
doc.Scintilla.ZoomFactor = 0;
}
private void toolStripButtonNewText_Click(object sender, EventArgs e)
{
if (dockPanel1.ActiveContent.GetType() == typeof(FormTreeLeft))
{
frTree.NewDoc();
}
else if (dockPanel1.ActiveContent.GetType() == typeof(YouDaoTree))
{
frYoudaoTree.NewDoc();
}
}
private void toolStripButtonNewDir_Click(object sender, EventArgs e)
{
if (dockPanel1.ActiveContent.GetType() == typeof(FormTreeLeft))
{
frTree.NewDir();
}
else if (dockPanel1.ActiveContent.GetType() == typeof(YouDaoTree))
{
frYoudaoTree.NewDir();
}
}
//保存
private void toolStripButtonSv_Click(object sender, EventArgs e)
{
if (ActiveDocument != null)
ActiveDocument.Save();
}
//设置路径,修改时间
public void SetTreeLocat(string treeLocation,string UpdateTime)
{
ActiveDocument.TreeLocation = treeLocation;
ActiveDocument.LastUpdateTime = UpdateTime;
}
//设置语言(激活文档)
public void SetLanguage(string language)
{
if ("ini".Equals(language, StringComparison.OrdinalIgnoreCase))
{
// Reset/set all styles and prepare _scintilla for custom lexing
ActiveDocument.IniLexer = true;
IniLexer.Init(ActiveDocument.Scintilla);
}
else
{
// Use a built-in lexer and configuration
ActiveDocument.IniLexer = false;
ActiveDocument.Scintilla.ConfigurationManager.Language = language;
// Smart indenting...
if ("cs".Equals(language, StringComparison.OrdinalIgnoreCase))
ActiveDocument.Scintilla.Indentation.SmartIndentType = ScintillaNET.SmartIndent.CPP;
else
ActiveDocument.Scintilla.Indentation.SmartIndentType = ScintillaNET.SmartIndent.None;
}
}
//设置语言
public void SetLanguageByDoc(string language,string id,string newTitle,string newTreeLocation)
{
//根据id设置语言
if (Attachment.isWelcomePageopen == "0")
{
foreach (DocumentForm documentForm in dockPanel1.Documents)
{
if (id.Equals(documentForm.NodeId, StringComparison.OrdinalIgnoreCase))
{
documentForm.SetLanguageByDoc(language,newTitle,newTreeLocation);
break;
}
}
}
}
//保存所有
private void toolStripButtonSvAll_Click(object sender, EventArgs e)
{
if (Attachment.isWelcomePageopen == "1")
{
return;
}
foreach (DocumentForm doc in dockPanel1.Documents)
{
doc.Activate();
doc.Save();
}
}
//新建数据库
private void toolStripMenuItemNewDB_Click(object sender, EventArgs e)
{
SaveFileDialog sf = new SaveFileDialog();
string path = "";
//设置文件类型
sf.Filter = "数据文件(*.mdb)|*.mdb";
if (sf.ShowDialog() == DialogResult.OK)
{
path = sf.FileName;
if (File.Exists(path)) //检查数据库是否已存在
{
throw new Exception("目标数据库已存在,无法创建");
}
// 可以加上密码,这样创建后的数据库必须输入密码后才能打开
path = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path;
// 创建一个CatalogClass对象的实例,
ADOX.CatalogClass cat = new ADOX.CatalogClass();
// 使用CatalogClass对象的Create方法创建ACCESS数据库
cat.Create(path);
//创建表
OleDbConnection conn = new OleDbConnection(path);
string crtSQL = " CREATE TABLE TTree ( " +
" [NodeId] INTEGER CONSTRAINT PK_TTree26 PRIMARY KEY, " +
" [Title] VARCHAR, " +
" [ParentId] INTEGER, " +
" [Type] INTEGER, " +
" [CreateTime] INTEGER, " +
" [SynId] INTEGER, " +
" [Turn] INTEGER, " +
" [MarkTime] INTEGER, " +
" [IsLock] INTEGER DEFAULT 0 ) ";
AccessAdo.ExecuteNonQuery(conn, crtSQL);
crtSQL = " CREATE TABLE TContent ( " +
" [NodeId] INTEGER CONSTRAINT PK_TTree27 PRIMARY KEY, " +
" [Content] MEMO, " +
" [Note] MEMO, " +
" [Link] MEMO, " +
" [UpdateTime] INTEGER ) ";
AccessAdo.ExecuteNonQuery(conn, crtSQL);
crtSQL = " CREATE TABLE TAttachment ( " +
" [AffixId] INTEGER CONSTRAINT PK_TTree28 PRIMARY KEY, " +
" [NodeId] INTEGER, " +
" [Title] VARCHAR, " +
" [Data] IMAGE , " +
" [Size] INTEGER, " +
" [Time] INTEGER)";
AccessAdo.ExecuteNonQuery(conn, crtSQL);
crtSQL = " CREATE TABLE MyKeys ( " +
" [KeyE] MEMO, " +
" [KeyD5] MEMO) ";
AccessAdo.ExecuteNonQuery(conn, crtSQL);
}
}
private bool closeAll()
{
//关闭所有打开的文档
string IsDocModi = "false";
if (Attachment.isWelcomePageopen == "0")
{
foreach (DocumentForm doc in dockPanel1.Documents)
{
if (doc.Scintilla.Modified)
IsDocModi = "true";
}
}
if (IsDocModi == "true")
{
DialogResult dr = MessageBox.Show(this, "是否保存所有文档?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (dr == DialogResult.Cancel)
{
return false;
}
else if (dr == DialogResult.No)
{
CloseAllDoment();
return true;
}
else
{
foreach (DocumentForm doc in dockPanel1.Documents)
{
doc.Save();
}
CloseAllDoment();
return true;
}
}
else
{
CloseAllDoment();
return true;
}
}
//关闭所有文档
private void CloseAllDoment()
{
if (Attachment.isWelcomePageopen == "0")
{
IDockContent[] documents = dockPanel1.DocumentsToArray();
foreach (IDockContent content in documents)
{
content.DockHandler.Close();
}
}
}
//打开数据库
private void toolStripMenuItemOpenDB_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
//openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "数据文件(*.mdb)|*.mdb";
openFileDialog1.RestoreDirectory = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//关闭所有打开的文档
if (closeAll() == false)
return;
//刷新附件列表数据
if (Attachment.isWelcomePageopen == "0")
{
openWelcomePage();
}
Attachment.ActiveNodeId = "-1";
Attachment.AttForm.ReFreshAttachGrid();
string fileName = openFileDialog1.FileName;
//修改连接字符串,并重新加载
string conStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + fileName;
UpdateConnectionStringsConfig("DBConn",conStr);
//重新加载所有资源
AccessAdo.strConnection = conStr;
//提示
DataTable tempdt = AccessAdo.ExecuteDataSet("select * from tcontent").Tables[0];
Boolean isNeedSyncExists = false;
for (int i = 0; i < tempdt.Columns.Count; i++)
{
if (tempdt.Columns[i].ColumnName == "NeedSync")
{
isNeedSyncExists = true;
break;
}
else
{
isNeedSyncExists = false;
}
}
if (isNeedSyncExists == true)
{
//打开的是缓存数据库,给予用户提示
MessageBox.Show("打开的是有道缓存数据库,对其进行的修改不会同步到云笔记!");
}
//升级数据库
CheckDb.UpdateDB();
frTree.frmTree_Reload();
ReSetMarkFind();
}
}
public void ReSetMarkFind()
{
//清空搜索重新加载书签
FormFind.IniData();
frmMark.RefreshGrid("local");
}
///<summary>
///更新连接字符串
///</summary>
///<param name="newName">连接字符串名称</param>
///<param name="newConString">连接字符串内容</param>
private static void UpdateConnectionStringsConfig(string newName,
string newConString)
{
bool isModified = false; //记录该连接串是否已经存在
//如果要更改的连接串已经存在
if (ConfigurationManager.ConnectionStrings[newName] != null)
{
isModified = true;
}
//新建一个连接字符串实例
ConnectionStringSettings mySettings =
new ConnectionStringSettings(newName, newConString);
// 打开可执行的配置文件*.exe.config
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
// 如果连接串已存在,首先删除它
if (isModified)
{
config.ConnectionStrings.ConnectionStrings.Remove(newName);
}
// 将新的连接串添加到配置文件中.
config.ConnectionStrings.ConnectionStrings.Add(mySettings);
// 保存对配置文件所作的更改
config.Save(ConfigurationSaveMode.Modified);
// 强制重新载入配置文件的ConnectionStrings配置节
ConfigurationManager.RefreshSection("ConnectionStrings");
}
//压缩数据库
private void toolStripMenuItemZipDB_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
//openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "数据文件(*.mdb)|*.mdb";
openFileDialog1.RestoreDirectory = false;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fileName = openFileDialog1.FileName;
//压缩
Compact(fileName);
}
}
///压缩修复ACCESS数据库,mdbPath为数据库绝对路径
public void Compact(string mdbPath)
{
if (!File.Exists(mdbPath)) //检查数据库是否已存在
{
throw new Exception("目标数据库不存在,无法压缩");
}
//声明临时数据库的名称
string temp = DateTime.Now.Year.ToString();
temp += DateTime.Now.Month.ToString();
temp += DateTime.Now.Day.ToString();
temp += DateTime.Now.Hour.ToString();
temp += DateTime.Now.Minute.ToString();
temp += DateTime.Now.Second.ToString() + ".bak";
temp = mdbPath.Substring(0, mdbPath.LastIndexOf("\\") + 1) + temp;
//定义临时数据库的连接字符串
string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp;
//定义目标数据库的连接字符串
string mdbPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
//创建一个JetEngineClass对象的实例
JRO.JetEngineClass jt = new JRO.JetEngineClass();
//使用JetEngineClass对象的CompactDatabase方法压缩修复数据库
jt.CompactDatabase(mdbPath2, temp2);
//拷贝临时数据库到目标数据库(覆盖)
File.Copy(temp, mdbPath, true);
//最后删除临时数据库
File.Delete(temp);
}
//备份当前数据库
private void toolStripMenuItemBackUpDB_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection(AccessAdo.strConnection);
string Path1 = conn.DataSource;
SaveFileDialog sf = new SaveFileDialog();
//设置文件类型
sf.Filter = "数据文件(*.mdb)|*.mdb";
if (sf.ShowDialog() == DialogResult.OK)
{
string Path2 = sf.FileName;
Backup(Path1, Path2);
}
}
/// 备份数据库,mdb1,源数据库绝对路径; mdb2: 目标数据库绝对路径
public void Backup(string mdb1, string mdb2)
{
if (!File.Exists(mdb1))
{
throw new Exception("源数据库不存在");
}
try
{
File.Copy(mdb1, mdb2, true);
}
catch (IOException ixp)
{
throw new Exception(ixp.ToString());
}
}
private void toolStripMenuItemUndo_Click(object sender, EventArgs e)
{
if (ActiveDocument != null)
ActiveDocument.Scintilla.UndoRedo.Undo();
}
private void toolStripMenuItemRedo_Click(object sender, EventArgs e)
{
if (ActiveDocument != null)
ActiveDocument.Scintilla.UndoRedo.Redo();
}
private void toolStripMenuItemCut_Click(object sender, EventArgs e)
{
if (ActiveDocument != null)
ActiveDocument.Scintilla.Clipboard.Cut();
}
private void toolStripMenuItemCopy_Click(object sender, EventArgs e)
{
if (ActiveDocument != null)
ActiveDocument.Scintilla.Clipboard.Copy();
}
private void toolStripMenuItempaste_Click(object sender, EventArgs e)
{
if (ActiveDocument != null)
ActiveDocument.Scintilla.Clipboard.Paste();
}
private void toolStripMenuItemFind_Click(object sender, EventArgs e)
{
if (ActiveDocument != null)