-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFrmPlugins.cs
More file actions
878 lines (798 loc) · 34.4 KB
/
FrmPlugins.cs
File metadata and controls
878 lines (798 loc) · 34.4 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
//-----------------------------------------------------------------------
// <copyright file="FrmPlugins.cs" company="NoteFly">
// NoteFly a note application.
// Copyright (C) 2010-2015 Tom
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// </copyright>
//-----------------------------------------------------------------------
namespace NoteFly
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;
/// <summary>
/// FrmPlugins window
/// </summary>
public partial class FrmPlugins : Form
{
/// <summary>
/// REST server domain.
/// </summary>
private const string RESTAPIDOMAIN = "//www.notefly.org/REST/";
/// <summary>
/// REST url where to get a list of plugins.
/// </summary>
private const string RESTAPIPLUGINSLIST = "plugins/list.php";
/// <summary>
/// REST url where to get the detail of a partialer plugin
/// </summary>
private const string RESTAPIPLUGINDETAILS = "plugins/details.php?name=";
/// <summary>
/// REST url where to get a list of matching pluginnames searched by a partialer keyword.
/// </summary>
private const string RESTAPIPLUGINSSEARCH = "plugins/search.php?keyword=";
/// <summary>
/// REST url where to get several versions of plugins in xml (with a http POST request).
/// </summary>
private const string RESTAPIPLUGINVERSION = "plugins/version.php";
/// <summary>
///
/// </summary>
private const string PLUGINMOREINFOURL = "//www.notefly.org/plugindetails?name=";
/// <summary>
/// Are plugin updates checkd by default
/// </summary>
private const bool PLUGINUPDATECHECKEDDEFAULT = true;
/// <summary>
/// The current selected plugin on the available plugins tabpage.
/// </summary>
private DownloadDetailsPlugin selectedplugindetails;
/// <summary>
/// All the plugins that can be updated.
/// </summary>
private List<DownloadDetailsPlugin> updatableplugins;
/// <summary>
/// Plugins positions in updatableplugins list that are being updated.
/// </summary>
private List<int> updatedplugins;
/// <summary>
/// Refence to FrmDownload window.
/// </summary>
private FrmDownloader frmdownloader;
/// <summary>
/// Number of dots after the loading text.
/// </summary>
private int textloadingdots = 0;
/// <summary>
/// The time the last request was made.
/// </summary>
private DateTime lastrequesttime;
/// <summary>
/// The tooltips of FrmPlugins.
/// </summary>
private ToolTip tooltip;
/// <summary>
/// Initializes a new instance of the <see cref="FrmPlugins" /> class.
/// </summary>
public FrmPlugins()
{
this.DoubleBuffered = Settings.ProgramFormsDoublebuffered;
this.InitializeComponent();
this.SetTabPageUpdatesVisible(false);
this.pluginGrid.DrawAllPluginsDetails(this.tabPagePluginsInstalled.ClientRectangle.Width);
this.SetFormTitle();
this.SetFormTooltips();
Strings.TranslateForm(this);
this.pluginGrid.Enabled = Settings.ProgramPluginsAllEnabled;
if (this.IsUpdatecheckPluginNeeded())
{
if (this.CheckPluginsUpdates())
{
Settings.UpdatecheckPluginsLastDate = DateTime.Now.ToString();
xmlUtil.WriteSettings();
}
}
this.linklblPluginMoreInfo.Text = Strings.T("more info");
this.lblPluginDescription.Text = Strings.T("Getting plugin details from the internet.");
}
/// <summary>
/// Set the title of this form.
/// </summary>
private void SetFormTitle()
{
this.RightToLeft = (RightToLeft)Settings.FontTextdirection;
this.Text = Strings.T("Plugins") + " - " + Program.AssemblyTitle;
}
/// <summary>
/// Set all form tooltips if tooltips are enabled.
/// </summary>
private void SetFormTooltips()
{
if (Settings.NotesTooltipsEnabled)
{
this.tooltip = new ToolTip(this.components);
this.searchtbPlugins.SetControlTooltip(this.tooltip);
this.pluginGrid.SetControlTooltip(this.tooltip);
}
else
{
if (this.tooltip != null)
{
this.tooltip.Active = false;
this.tooltip.Dispose();
}
}
}
/// <summary>
/// Check if there is a update check for the plugins needed.
/// </summary>
/// <returns>true if Settings.UpdatecheckPluginsLastDate + Settings.UpdatecheckPluginsEverydays more than current datetime.</returns>
private bool IsUpdatecheckPluginNeeded()
{
bool pluginupdatecheckneeded = false;
if (Settings.UpdatecheckPluginsEverydays > 0)
{
DateTime lastupdateplugins = DateTime.Parse(Settings.UpdatecheckPluginsLastDate);
if (lastupdateplugins.AddDays(Settings.UpdatecheckPluginsEverydays) <= DateTime.Now)
{
pluginupdatecheckneeded = true;
}
}
return pluginupdatecheckneeded;
}
/// <summary>
/// Check if tabPagePluginsAvailable is selected,
/// if it is then get a list with available all plugins.
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
private void tabControlPlugins_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.tabControlPlugins.SelectedTab == this.tabPagePluginsAvailable)
{
this.lblTextNoInternetConnection.Visible = false;
this.splitContainerAvailablePlugins.Panel2Collapsed = true;
if (!this.searchtbPlugins.IsKeywordEntered)
{
HttpUtil httputil_allplugins = new HttpUtil(RESTAPIDOMAIN + RESTAPIPLUGINSLIST, System.Net.Cache.RequestCacheLevel.Revalidate, null);
if (!httputil_allplugins.Start(new System.ComponentModel.RunWorkerCompletedEventHandler(this.httputil_allplugins_DownloadCompleet)))
{
this.SetAvailablePluginsNetwork(false);
}
else
{
this.SetAvailablePluginsNetwork(true);
}
}
}
}
/// <summary>
/// Downloading of a list of allplugins is compleet, parser results and display items in chlbxAvailiblePlugins.
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">RunWorkerCompletedEvent Arguments</param>
private void httputil_allplugins_DownloadCompleet(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
string response = (string)e.Result;
this.lbxAvailablePlugins.Items.Clear();
this.lbxAvailablePlugins.Enabled = true;
if (!xmlUtil.ParserListPlugins(response, PluginsManager.GetIPluginVersion(), this.lbxAvailablePlugins))
{
this.SetAvailablePluginsNetwork(false);
}
else
{
this.SetAvailablePluginsNetwork(true);
}
}
/// <summary>
/// A plugin is selected, get the details from the selected plugin.
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
private void lbxAvailablePlugins_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.lbxAvailablePlugins.SelectedIndex >= 0)
{
if (this.RateLimitRequest())
{
return;
}
this.lastrequesttime = DateTime.Now;
this.splitContainerAvailablePlugins.Panel2Collapsed = false;
this.btnPluginDownload.Visible = false;
string pluginname = this.lbxAvailablePlugins.SelectedItem.ToString();
if (string.IsNullOrEmpty(pluginname))
{
Log.Write(LogType.error, "Empty plugin name.");
return;
}
if (pluginname.Equals("Loading...", StringComparison.Ordinal))
{
Log.Write(LogType.error, "Should not select plugin yet.");
return;
}
HttpUtil httputil_plugindetail = new HttpUtil(RESTAPIDOMAIN + RESTAPIPLUGINDETAILS + System.Web.HttpUtility.UrlEncode(pluginname), System.Net.Cache.RequestCacheLevel.Revalidate);
this.ClearPluginDetails();
this.timerTextUpdaterLoading.Start();
if (!httputil_plugindetail.Start(new RunWorkerCompletedEventHandler(this.httputil_plugindetail_DownloadCompleet)))
{
this.SetAvailablePluginsNetwork(false);
}
else
{
this.SetAvailablePluginsNetwork(true);
}
}
}
/// <summary>
/// Check if request is not too fast after previous one.
/// Avoid REST api silently refuses to answer.
/// </summary>
/// <returns>True if request is too fast.</returns>
private bool RateLimitRequest()
{
const int MINDIFFREQTIME = 400;
if (this.lastrequesttime != null)
{
if (this.lastrequesttime.AddMilliseconds(MINDIFFREQTIME).CompareTo(DateTime.Now) > 0)
{
return true;
}
}
return false;
}
/// <summary>
/// Hide all plugins details
/// </summary>
private void ClearPluginDetails()
{
this.lblPluginName.ResetText();
this.lblLicense.ResetText();
this.lblPluginVersion.ResetText();
this.lblPluginDescription.ResetText();
this.linklblPluginMoreInfo.Visible = false;
}
/// <summary>
/// Downloading of plugin details data is compleet, parser data and display plugin details
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">RunWorkerCompleted event arguments</param>
private void httputil_plugindetail_DownloadCompleet(object sender, RunWorkerCompletedEventArgs e)
{
string response = (string)e.Result;
if (String.IsNullOrEmpty(response))
{
return;
}
this.timerTextUpdaterLoading.Stop();
this.ClearPluginDetails();
this.selectedplugindetails = xmlUtil.ParserDetailsPlugin(response);
if (this.selectedplugindetails != null)
{
if (this.selectedplugindetails.IsInstalledPlugin())
{
this.btnPluginDownload.Enabled = false;
if (this.selectedplugindetails.IsNewerVersion())
{
this.btnPluginDownload.Text = Strings.T("needs update");
if (this.updatableplugins == null)
{
this.updatableplugins = new List<DownloadDetailsPlugin>();
}
if (!this.UpdateablePluginsContainsPlugin(this.selectedplugindetails))
{
this.updatableplugins.Add(this.selectedplugindetails);
this.chxlbxPluginUpdates.Items.Add(this.selectedplugindetails.Name, PLUGINUPDATECHECKEDDEFAULT);
this.SetTabPageUpdatesVisible(true);
}
}
else
{
this.btnPluginDownload.Text = Strings.T("already installed");
}
}
else
{
this.btnPluginDownload.Enabled = true;
this.btnPluginDownload.Text = Strings.T("download");
}
if (!string.IsNullOrEmpty(this.selectedplugindetails.DownloadUrl) && Uri.IsWellFormedUriString(this.selectedplugindetails.DownloadUrl, UriKind.Absolute))
{
this.btnPluginDownload.Visible = true;
}
this.lblPluginName.Text = this.selectedplugindetails.Name;
this.lblPluginVersion.Text = Strings.T("version: ") + this.selectedplugindetails.Version;
this.lblLicense.Text = Strings.T("license: ") + this.selectedplugindetails.LicenseType;
this.lblPluginDescription.Text = this.selectedplugindetails.Description;
this.linklblPluginMoreInfo.Visible = true;
if (Settings.NotesTooltipsEnabled)
{
this.tooltip.SetToolTip(this.linklblPluginMoreInfo, Strings.T("visit: {0}", PLUGINMOREINFOURL + Uri.EscapeUriString(this.selectedplugindetails.Name)));
}
}
else
{
this.lblPluginDescription.Text = Strings.T("Could not load plugins details, please check if your network connection is still working.");
this.linklblPluginMoreInfo.Visible = false;
}
}
/// <summary>
///
/// </summary>
/// <param name="pluginname"></param>
/// <returns></returns>
private bool UpdateablePluginsContainsPlugin(DownloadDetailsPlugin plugin)
{
for (int i = 0; i < this.updatableplugins.Count; i++)
{
if (this.updatableplugins[i].DownloadUrl == plugin.DownloadUrl)
{
return true;
}
}
return false;
}
/// <summary>
/// Start download of plugin.
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
private void btnPluginDownload_Click(object sender, EventArgs e)
{
if (this.selectedplugindetails != null)
{
this.frmdownloader = new FrmDownloader(Strings.T("Downloading plugin.."));
this.frmdownloader.AllDownloadsCompleted += new FrmDownloader.DownloadCompleetHandler(this.downloader_DownloadCompleet);
this.frmdownloader.Show();
this.frmdownloader.BeginDownload(this.selectedplugindetails.DownloadUrl, Settings.ProgramPluginsFolder);
}
}
/// <summary>
/// Downloading of plugin compleet.
/// </summary>
/// <param name="newfiles">Array of files downloads.</param>
private void downloader_DownloadCompleet(string[] newpluginfile)
{
RSAVerify rsaverify = new RSAVerify();
if (!rsaverify.CheckFileSignatureAndDisplayErrors(newpluginfile[0], this.selectedplugindetails.Signature))
{
return;
}
this.DecompressDownload(newpluginfile[0]);
PluginsManager.LoadPlugins();
this.pluginGrid.DrawAllPluginsDetails(this.tabPagePluginsInstalled.ClientRectangle.Width);
this.tabControlPlugins.SelectedTab = this.tabPagePluginsInstalled;
}
/// <summary>
/// Downloading of plugin updates has been compleeted.
/// </summary>
/// <param name="newpluginfile"></param>
private void DownloadUpdatesPluginsCompleet(string[] compressedpluginfiles)
{
bool updatesinstalled = false;
RSAVerify rsaverify = new RSAVerify();
for (int i = 0; i < compressedpluginfiles.Length; i++)
{
int pluginposition = this.updatedplugins[i];
if (rsaverify.CheckFileSignatureAndDisplayErrors(compressedpluginfiles[i], this.updatableplugins[pluginposition].Signature))
{
this.DecompressDownload(compressedpluginfiles[i]);
updatesinstalled = true;
}
}
for (int p = this.updatedplugins.Count - 1; p >= 0; p--)
{
this.chxlbxPluginUpdates.Items.RemoveAt(p);
}
if (updatesinstalled)
{
PluginsManager.LoadPlugins();
this.pluginGrid.DrawAllPluginsDetails(this.tabPagePluginsInstalled.ClientRectangle.Width);
this.btnRestartProgram.Visible = true;
}
if (this.chxlbxPluginUpdates.Items.Count > 0)
{
this.btnUpdatePlugins.Enabled = true;
}
this.chxlbxPluginUpdates.Enabled = true;
}
/// <summary>
/// Decompress the downloaded file.
/// </summary>
/// <param name="newfiles"></param>
private void DecompressDownload(string compressedpluginfile)
{
if (this.frmdownloader.GetFileCompressedkind(compressedpluginfile) == 1)
{
string[] unzipextensions = new string[1] { ".dll" };
if (this.frmdownloader.DecompressZipFile(compressedpluginfile, unzipextensions))
{
// decompress succesfully, now delete zip file
this.DeleteNotsysFile(compressedpluginfile, "Delete zip archive: ");
}
}
else if (this.frmdownloader.GetFileCompressedkind(compressedpluginfile) == 2)
{
if (this.frmdownloader.DecompressGZipFile(compressedpluginfile))
{
// decompress succesfully, now delete gzip file
this.DeleteNotsysFile(compressedpluginfile, "Delete GZip file: ");
}
}
}
/// <summary>
/// Check if file is not a system file, and if it's not then delete the file.
/// </summary>
/// <param name="file">The file to delete</param>
/// <param name="logdesc">Log mesage</param>
private void DeleteNotsysFile(string file, string logdesc)
{
if (System.IO.File.GetAttributes(file) != System.IO.FileAttributes.System)
{
try
{
File.Delete(file);
Log.Write(LogType.info, logdesc + file);
}
catch (ArgumentException argexc)
{
Log.Write(LogType.exception, argexc.Message);
}
catch (IOException ioexc)
{
Log.Write(LogType.exception, ioexc.Message);
}
}
}
/// <summary>
/// The user request to search, make request for search results.
/// </summary>
/// <param name="keywords">The keywords</param>
private void searchtbPlugins_SearchStart(string keywords)
{
this.lbxAvailablePlugins.Items.Clear();
HttpUtil httputil_searchplugins = new HttpUtil(RESTAPIDOMAIN + RESTAPIPLUGINSSEARCH + System.Web.HttpUtility.UrlEncode(keywords), System.Net.Cache.RequestCacheLevel.Revalidate, null);
this.ClearPluginDetails();
this.splitContainerAvailablePlugins.Panel2Collapsed = true;
if (!httputil_searchplugins.Start(new System.ComponentModel.RunWorkerCompletedEventHandler(this.httputil_searchplugins_DownloadCompleet)))
{
this.SetAvailablePluginsNetwork(false);
}
else
{
this.SetAvailablePluginsNetwork(true);
}
}
/// <summary>
/// Downloading of search results data is compleet, parser searchresult data and list search results
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">RunWorkerCompleted Event Arguments</param>
private void httputil_searchplugins_DownloadCompleet(object sender, RunWorkerCompletedEventArgs e)
{
string response = (string)e.Result;
this.lbxAvailablePlugins.BeginUpdate();
if (!xmlUtil.ParserListPlugins(response, PluginsManager.GetIPluginVersion(), this.lbxAvailablePlugins))
{
this.searchtbPlugins.Enabled = false;
}
this.lbxAvailablePlugins.EndUpdate();
}
/// <summary>
/// The user wants to stop searching, get the list of all plugins again.
/// </summary>
private void searchtbPlugins_SearchStop()
{
HttpUtil httputil_allplugins = new HttpUtil(RESTAPIDOMAIN + RESTAPIPLUGINSLIST, System.Net.Cache.RequestCacheLevel.Revalidate);
if (!httputil_allplugins.Start(new RunWorkerCompletedEventHandler(this.httputil_allplugins_DownloadCompleet)))
{
this.SetAvailablePluginsNetwork(false);
}
else
{
this.SetAvailablePluginsNetwork(true);
}
}
/// <summary>
/// Set a message if network connection failed.
/// </summary>
/// <param name="isconnected">True if their is a network connection.</param>
private void SetAvailablePluginsNetwork(bool isconnected)
{
this.lblTextNoInternetConnection.Visible = !isconnected;
this.searchtbPlugins.Enabled = isconnected;
if (!isconnected)
{
string nointernetconnectionmessage = Strings.T("Could not load list with plugins. Internet connection failed.");
if (Settings.NetworkProxyEnabled)
{
nointernetconnectionmessage += Environment.NewLine + Strings.T("A proxy is being used.");
}
this.lblTextNoInternetConnection.Text = nointernetconnectionmessage;
Log.Write(LogType.info, nointernetconnectionmessage);
this.lbxAvailablePlugins.Items.Clear();
}
}
/// <summary>
/// Check plugins on updates.
/// </summary>
private bool CheckPluginsUpdates()
{
string[] installedpluginsnames = PluginsManager.GetInstalledPlugins();
StringBuilder sbpluginspostsparam = new StringBuilder("plugins=");
for (int i = 0; i < installedpluginsnames.Length; i++)
{
if (i != 0)
{
sbpluginspostsparam.Append(",");
}
sbpluginspostsparam.Append(System.Web.HttpUtility.UrlEncode(installedpluginsnames[i]));
}
if (sbpluginspostsparam.Length < 1)
{
return true;
}
HttpUtil httputil = new HttpUtil(RESTAPIDOMAIN + RESTAPIPLUGINVERSION, System.Net.Cache.RequestCacheLevel.Revalidate, sbpluginspostsparam.ToString());
if (httputil.Start(new RunWorkerCompletedEventHandler(this.httputil_pluginsversions_compleet)))
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// Parser results of versions requested plugins, and set tabpage with plugin update visible
/// if there are plugin update(s) are available.
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
private void httputil_pluginsversions_compleet(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
string result = (string)e.Result;
if (result == null)
{
return;
}
if (result.Length < 1)
{
return;
}
this.updatableplugins = new List<DownloadDetailsPlugin>();
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(result);
XmlNodeList xmlnodelist = xmldoc.SelectNodes("/plugins/plugin");
foreach (XmlNode xmlnode in xmlnodelist)
{
if (xmlnode.ChildNodes.Count == 1)
{
const int MAXLENMESSAGE = 500;
string errormessage = xmlnode.ChildNodes[0].InnerText;
if (errormessage.Length > MAXLENMESSAGE)
{
errormessage = errormessage.Substring(0, MAXLENMESSAGE);
}
Log.Write(LogType.error, errormessage);
}
else if (xmlnode.ChildNodes.Count > 1)
{
bool minversionipluginokay = true;
DownloadDetailsPlugin downloaddetailsplugin = new DownloadDetailsPlugin();
for (int i = 0; (i < xmlnode.ChildNodes.Count && minversionipluginokay); ++i)
{
switch (xmlnode.ChildNodes[i].Name)
{
case "name":
downloaddetailsplugin.Name = xmlnode.ChildNodes[i].InnerText;
break;
case "version":
downloaddetailsplugin.Version = xmlnode.ChildNodes[i].InnerText;
break;
case "downloadurl":
downloaddetailsplugin.DownloadUrl = xmlnode.ChildNodes[i].InnerText;
break;
case "signature":
downloaddetailsplugin.Signature = xmlnode.ChildNodes[i].InnerText;
break;
case "minversioniplugin":
short[] pluginipluginversionneeded = new short[] { 0,0,0};
try
{
pluginipluginversionneeded = Program.ParserVersionString(xmlnode.ChildNodes[i].InnerText);
}
catch (Exception)
{
Log.Write(LogType.error, "Server returned unknown minversioniplugin.");
}
if (Program.CompareVersions(pluginipluginversionneeded, PluginsManager.GetIPluginVersion()) > 1)
{
minversionipluginokay = false;
}
break;
}
}
if (downloaddetailsplugin.IsInstalledPlugin() && minversionipluginokay)
{
if (downloaddetailsplugin.IsNewerVersion())
{
this.updatableplugins.Add(downloaddetailsplugin);
}
}
}
}
if (this.updatableplugins.Count > 0)
{
this.SetTabPageUpdatesVisible(true);
this.tabControlPlugins.SelectedTab = this.tabPagePluginsUpdates;
}
for (int i = 0; i < this.updatableplugins.Count; i++)
{
this.chxlbxPluginUpdates.Items.Add(this.updatableplugins[i].Name, PLUGINUPDATECHECKEDDEFAULT);
}
}
/// <summary>
/// Button clicked to update plugins.
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
private void btnupdateplugins_Click(object sender, EventArgs e)
{
this.btnUpdatePlugins.Enabled = false;
this.updatedplugins = new List<int>();
this.frmdownloader = new FrmDownloader(Strings.T("Updating plugins.."));
this.chxlbxPluginUpdates.Enabled = false;
for (int i = 0; i < this.chxlbxPluginUpdates.Items.Count; i++)
{
if (this.chxlbxPluginUpdates.GetItemChecked(i))
{
// checked for update plugin
if (this.updatableplugins[i] == null)
{
continue;
}
this.updatedplugins.Add(i);
}
}
this.frmdownloader.Show();
this.frmdownloader.AllDownloadsCompleted += new FrmDownloader.DownloadCompleetHandler(this.DownloadUpdatesPluginsCompleet);
string[] alldownloadurls = this.GetAllDownloadUrls(updatedplugins.ToArray());
this.frmdownloader.BeginDownload(alldownloadurls, Program.GetNewPluginFolder(true));
}
/// <summary>
/// Gets a array with all downloadurls in the updatableplugins list.
/// </summary>
/// <param name="updatedplugins">Array with positions in updateableplugins list to be updated.</param>
/// <returns>Array with all download urls of plugins to be updated.</returns>
private string[] GetAllDownloadUrls(int[] updatedplugins)
{
string[] alldownloadurls = new string[updatedplugins.Length];
for (int i = 0; i < updatedplugins.Length; i++)
{
int pluginposition = updatedplugins[i];
alldownloadurls[i] = this.updatableplugins[pluginposition].DownloadUrl;
}
return alldownloadurls;
}
/// <summary>
/// Update the number of dots in after the loading text.
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
private void timerTextUpdater_Tick(object sender, EventArgs e)
{
const int MAXTEXTLOADINGDOTS = 5;
StringBuilder text = new StringBuilder(Strings.T("Loading"));
for (int i = 0; i < this.textloadingdots; i++)
{
text.Append(".");
}
this.lblPluginName.Text = text.ToString();
this.textloadingdots++;
if (this.textloadingdots > MAXTEXTLOADINGDOTS)
{
this.textloadingdots = 0;
}
}
/// <summary>
/// Set the this.tabPagePluginsUpdates visible or invisible.
/// </summary>
/// <param name="showupdatetab">True if update tab is visible.</param>
private void SetTabPageUpdatesVisible(bool showupdatetab)
{
if ((showupdatetab) && (!this.tabControlPlugins.TabPages.Contains(this.tabPagePluginsUpdates)))
{
this.tabControlPlugins.TabPages.Add(this.tabPagePluginsUpdates);
}
else if ((!showupdatetab) && this.tabControlPlugins.TabPages.Contains(this.tabPagePluginsUpdates))
{
this.tabControlPlugins.TabPages.Remove(this.tabPagePluginsUpdates);
}
}
/// <summary>
/// Restart this programme
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
private void btnRestartProgram_Click(object sender, EventArgs e)
{
bool continuerestart = false;
if (Program.Formmanager.Frmneweditnoteopen)
{
DialogResult dlgrescontinu = MessageBox.Show(Strings.T("A note is still open for editing continue restarting {0} now? The unsaved note change will be lost.", Program.AssemblyTitle), "", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
if (dlgrescontinu == System.Windows.Forms.DialogResult.Yes)
{
continuerestart = true;
}
}
else
{
continuerestart = true;
}
if (continuerestart)
{
Program.DisposeTrayicon();
Application.Restart();
}
}
/// <summary>
/// An plugin in the updatable plugin checkboxlistbox is check or unchecked.
/// Disable the btnupdateplugins button if no plugin is checked.
/// </summary>
/// <param name="sender">Sender object</param>
/// <param name="e">Event arguments</param>
private void chxlbxPluginUpdates_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
this.btnUpdatePlugins.Enabled = true;
}
else
{
bool anychecked = false;
for (int i = 0; i < this.chxlbxPluginUpdates.Items.Count; i++)
{
if (this.chxlbxPluginUpdates.GetItemChecked(i) && e.Index != i)
{
anychecked = true;
break;
}
}
this.btnUpdatePlugins.Enabled = anychecked;
}
}
/// <summary>
/// More info plugin clicked.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void linklblPluginMoreInfo_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
if (this.selectedplugindetails == null)
{
return;
}
if (!String.IsNullOrEmpty(this.selectedplugindetails.Name))
{
Program.LoadLink(PLUGINMOREINFOURL + Uri.EscapeUriString(this.selectedplugindetails.Name), false);
}
}
}
}