-
Notifications
You must be signed in to change notification settings - Fork 0
/
Setting.cs
133 lines (110 loc) · 3.78 KB
/
Setting.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using Microsoft.Win32;
namespace ClipNote
{
[XmlRoot(ElementName = "ClipNoteConfig")]
public class Setting
{
[XmlElement(ElementName = "AccessToken")]
public string AccessToken;
[XmlElement(ElementName = "AccessTokenSecret")]
public string AccessTokenSecret;
[XmlElement(ElementName = "PageUrl")]
public string pageUrl;
[XmlElement(ElementName = "NoteName")]
public string noteName;
[XmlElement(ElementName = "PageId")]
public string pageId;
private Springnote.Consumer consumer;
private string FilePath { get; set; }
Setting()
{
}
public void Initialize(string path)
{
OAuth.OAuthToken consumerToken = new OAuth.OAuthToken("LDlaQSUySTwDAuyNG7dUQ", "rOEqilylc2zYqBoRjmJjx61GgeLitb6Kf35dePbE");
OAuth.OAuthToken accessToken = IsLoggedIn() ? new OAuth.OAuthToken(AccessToken, AccessTokenSecret) : null;
this.consumer = new Springnote.Consumer(consumerToken, accessToken);
FilePath = path;
}
public static Setting Load(string root)
{
string path = root + "\\clipnote.config.xml";
Setting ret;
try
{
XmlSerializer serializer = new XmlSerializer(typeof(Setting));
FileStream stream = new FileStream(path, FileMode.Open);
ret = (Setting)serializer.Deserialize(stream);
stream.Close();
}
catch(IOException)
{
ret = new Setting();
}
ret.Initialize(path);
return ret;
}
public void ClearSession()
{
this.AccessToken = String.Empty;
this.AccessTokenSecret = String.Empty;
this.pageId = String.Empty;
this.pageUrl = String.Empty;
this.pageId = String.Empty;
Save();
}
public Springnote.Consumer GetConsumer()
{
return consumer;
}
public Springnote.Note GetNote()
{
return new Springnote.Note(noteName, GetConsumer());
}
public Springnote.Page GetPage()
{
return GetNote().FindPage(pageId);
}
public void Save()
{
XmlSerializer serializer = new XmlSerializer(this.GetType());
XmlWriter writer = new XmlTextWriter(FilePath, Encoding.UTF8);
serializer.Serialize(writer, this);
writer.Close();
}
public Boolean IsLoggedIn()
{
return this.AccessToken != null && this.AccessToken.Length > 0 && this.AccessTokenSecret != null && this.AccessTokenSecret.Length > 0;
}
public void Authorize()
{
consumer.GetRequestToken();
consumer.Authorize(null);
}
public Boolean GetAccessToken()
{
consumer.GetAccessToken();
AccessToken = consumer.accessToken.TokenKey;
AccessTokenSecret = consumer.accessToken.TokenSecret;
Save();
return true;
}
public void SetAutorun(string path)
{
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("ClipNote", "\"" + path + "\" /minimize");
}
public void ClearAutorun()
{
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.DeleteValue("ClipNote", false);
}
}
}