lrbalt / outlookemailtotracks

Add an email to Tracks from Outlook using an Outlook plugin

outlookemailtotracks / TodoForm10.frm
100644 348 lines (283 sloc) 9.739 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
VERSION 5.00
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} TodoForm
   Caption = "Tracks Todo"
   ClientHeight = 4725
   ClientLeft = 45
   ClientTop = 330
   ClientWidth = 7035
   OleObjectBlob = "TodoForm10.frx":0000
   StartUpPosition = 1 'CenterOwner
End
Attribute VB_Name = "TodoForm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
 
 
Option Explicit
 
' greg jarman 20080821 v1.0 initial version
'
'
 
' the URL of the tracks installation, required.
Const sURL = "http://your.tracks.host/tracks/"
 
' set username and password here, required.
Const sUsername = "userid"
Const sPassword = "password"
 
' proxy server address and port number, in the form proxy.server.com:1234. Set to "" for none
Const sProxy = ""
 
' set this to true if you want to refresh the projects and contexts each time a new todo is created.
' otherwise the data is gathered the first time a todo is created after outlook is opened.
Const Update_Projects_And_Contexts_Each_Time = True
 
' internal variables
Private Type Context
  Name As String
  id As Integer
  Position As Integer
End Type
 
Private Type Project
  Name As String
  id As Integer
  Position As Integer
  State As String
End Type
 
Dim Projects() As Project
Dim ActiveProjects() As Project
Dim Projects_Length As Integer
Public Projects_Loaded As Boolean
 
Dim Contexts() As Context
Dim Contexts_Length As Integer
Public Contexts_Loaded As Boolean
 
Const HTTPREQUEST_SETCREDENTIALS_FOR_SERVER = 0
 
 
Private Function CreateWinHttpRequest() As Object
  Dim WinHttpRequest As Object
  
  Set WinHttpRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
  
  Set CreateWinHttpRequest = WinHttpRequest
End Function
 
Private Sub ConfigureWinHttpRequest(WinHttpRequest As Variant)
  WinHttpRequest.SetCredentials sUsername, sPassword, HTTPREQUEST_SETCREDENTIALS_FOR_SERVER
  WinHttpRequest.SetRequestHeader "Content-type", "text/xml"
  If Len(sProxy) > 0 Then
    WinHttpRequest.SetProxy 2, sProxy, ""
  End If
  'WinHttpRequest.SetClientCertificate ("")
End Sub
 
' Download the contexts from the Tracks server and add them to the Projects array
Private Sub DownloadContexts()
  Dim WinHttpRequest As Object
  Dim XMLDoc As Object
  Dim oRoot As Object
  Dim oContext As Object
  Dim oChild As Object
  
  Set WinHttpRequest = CreateWinHttpRequest()
  WinHttpRequest.Open "GET", sURL & "contexts.xml", False
  ConfigureWinHttpRequest WinHttpRequest
  WinHttpRequest.Send
    
  If Not WinHttpRequestSucceeded(WinHttpRequest) Then
    MsgBox "DownloadContexts Failed: " & WinHttpRequest.StatusText
    Exit Sub
  End If
    
    Set XMLDoc = CreateObject("MSXML2.DOMDocument")
    XMLDoc.validateOnParse = False
    XMLDoc.loadXML (WinHttpRequest.ResponseText)
    Set oRoot = XMLDoc.documentElement
    Contexts_Length = oRoot.childNodes.Length
    ReDim Contexts(0 To Contexts_Length - 1) As Context
    
    Dim sName
    Dim iPosition
    Dim iId
    For Each oContext In oRoot.childNodes
      For Each oChild In oContext.childNodes
        If oChild.nodeName = "name" Then
          sName = oChild.nodeTypedValue
        ElseIf oChild.nodeName = "position" Then
          iPosition = Val(oChild.nodeTypedValue)
        ElseIf oChild.nodeName = "id" Then
          iId = Val(oChild.nodeTypedValue)
        End If
      Next oChild
      Contexts(iPosition - 1).Name = sName
      Contexts(iPosition - 1).Position = iPosition
      Contexts(iPosition - 1).id = iId
    Next oContext
    
    Contexts_Loaded = True
    
    Set WinHttpRequest = Nothing
    Set oRoot = Nothing
    Set XMLDoc = Nothing
End Sub
 
' Download the projects from the Tracks server and add them to the Projects array
Private Sub DownloadProjects()
  Dim WinHttpRequest As Object
  Dim XMLDoc As Object
  Dim oRoot As Object
  Dim oProject As Object
  Dim oChild As Object
  
    Set WinHttpRequest = CreateWinHttpRequest()
    WinHttpRequest.Open "GET", sURL & "projects.xml", False
    ConfigureWinHttpRequest WinHttpRequest
    WinHttpRequest.Send
    
    If Not WinHttpRequestSucceeded(WinHttpRequest) Then
      MsgBox "DownloadProjects Failed: " & WinHttpRequest.StatusText
      Exit Sub
    End If
  
    Set XMLDoc = CreateObject("MSXML2.DOMDocument")
    XMLDoc.validateOnParse = False
    XMLDoc.loadXML (WinHttpRequest.ResponseText)
    Set oRoot = XMLDoc.documentElement
    Projects_Length = oRoot.childNodes.Length
    ReDim Projects(0 To Projects_Length - 1) As Project
    
    Dim sName
    Dim iPosition
    Dim iId
    Dim sState
    For Each oProject In oRoot.childNodes
      For Each oChild In oProject.childNodes
        If oChild.nodeName = "name" Then
          sName = oChild.nodeTypedValue
        ElseIf oChild.nodeName = "position" Then
          iPosition = Val(oChild.nodeTypedValue)
        ElseIf oChild.nodeName = "id" Then
          iId = Val(oChild.nodeTypedValue)
        ElseIf oChild.nodeName = "state" Then
          sState = oChild.nodeTypedValue
        End If
      Next oChild
      Projects(iPosition - 1).Name = sName
      Projects(iPosition - 1).Position = iPosition
      Projects(iPosition - 1).id = iId
      Projects(iPosition - 1).State = sState
    Next oProject
    
    Projects_Loaded = True
    
    Set WinHttpRequest = Nothing
    Set oRoot = Nothing
    Set XMLDoc = Nothing
End Sub
 
 
 
Private Sub PopulateProjectListBox()
  Dim old_index As Integer
  
  old_index = TodoForm.ProjectListBox.ListIndex
  TodoForm.ProjectListBox.Clear
  TodoForm.ProjectListBox.Style = fmStyleDropDownList
    
  Dim i
  Dim count As Integer
  ReDim ActiveProjects(0 To Projects_Length - 1)
  count = 0
  
  For i = 0 To Projects_Length - 1
    If Projects(i).State = "active" Then
      TodoForm.ProjectListBox.AddItem Projects(i).Name
      ActiveProjects(count) = Projects(i)
      count = count + 1
    End If
  Next i
  
  TodoForm.ProjectListBox.ListIndex = old_index
End Sub
 
Private Sub PopulateContextListBox()
  Dim old_index As Integer
  old_index = TodoForm.ContextListBox.ListIndex
    
  TodoForm.ContextListBox.Clear
  TodoForm.ContextListBox.Style = fmStyleDropDownList
    
  Dim i
  For i = 0 To Contexts_Length - 1
    TodoForm.ContextListBox.AddItem Contexts(i).Name
  Next i
  
  TodoForm.ContextListBox.ListIndex = old_index
End Sub
 
Private Function WinHttpRequestSucceeded(WinHttpRequest As Variant)
  WinHttpRequestSucceeded = (WinHttpRequest.Status >= 200) And (WinHttpRequest.Status <= 299)
End Function
 
Private Function HTMLEncode(ByVal Text As String, Optional HardSpaces As Boolean = False) As String
  Dim i As Integer
  Dim ch As String
  Dim NewString As String
 
 
  For i = 1 To Len(Text)
    ch = Mid$(Text, i, 1)
    Select Case ch
          Case " "
              If HardSpaces Then ch = "&nbsp;"
          Case """"
              ch = "&quot;"
          Case "&"
                ch = "&amp;"
          Case "<"
              ch = "&lt;"
          Case ">"
              ch = "&gt;"
          Case " " To "~"
              ' Not one we already processed but
              ' but in the normal display range
          Case Else
              ch = "&#" & Asc(ch) & ";"
      End Select
      NewString = NewString & ch
  Next
  HTMLEncode = NewString
End Function
 
Private Sub CreateTodo(Description As String, Notes As String, ContextId As Integer, ProjectId As Integer)
  Dim WinHttpRequest As Object
  Dim sData As String
  Dim aBody() As Byte
    
  Set WinHttpRequest = CreateWinHttpRequest()
  WinHttpRequest.Open "POST", sURL & "todos.xml", False
  ConfigureWinHttpRequest WinHttpRequest
    
  sData = "<todo><description>" + Description + "</description>"
  sData = sData & "<notes>" & Notes & "</notes>"
  sData = sData & "<context_id>" & Str(ContextId) & "</context_id>"
    
  If ProjectId > 0 Then
    sData = sData & "<project_id>" & Str(ProjectId) & "</project_id>"
  End If
 
  sData = sData & "</todo>" & vbCrLf
    
  aBody = StrConv(sData, vbFromUnicode)
  WinHttpRequest.Send CByte(aBody)
    
  If Not WinHttpRequestSucceeded(WinHttpRequest) Then
    MsgBox "CreateTodo Failed: " & WinHttpRequest.StatusText
  End If
    
  Set WinHttpRequest = Nothing
End Sub
 
Private Sub AddActionButton_Click()
  If Len(DescriptionTextBox.Text) = 0 Then
    MsgBox "Must have a description!", vbExclamation
  ElseIf ContextListBox.ListIndex = -1 Then
    MsgBox "Must have a context!", vbExclamation
  Else
    Dim ContextId As Integer
    Dim ProjectId As Integer
    ContextId = Contexts(ContextListBox.ListIndex).id
    ProjectId = 0
    If ProjectListBox.ListIndex > 0 Then
      ProjectId = ActiveProjects(ProjectListBox.ListIndex).id
    End If
    
    CreateTodo DescriptionTextBox.Text, NotesTextBox.Text, ContextId, ProjectId
    FormReset
    TodoForm.Hide
  End If
End Sub
 
Private Sub FormReset()
  DescriptionTextBox.Text = ""
  NotesTextBox.Text = ""
End Sub
 
Private Sub CancelButton_Click()
  FormReset
  TodoForm.Hide
End Sub
 
 
Private Sub ClearProject_Click()
  ProjectListBox.ListIndex = -1
End Sub
 
Private Sub UserForm_Initialize()
  Contexts_Loaded = False
  Projects_Loaded = False
End Sub
 
Private Sub UserForm_Activate()
  If (Projects_Loaded = False) Or Update_Projects_And_Contexts_Each_Time Then
    DownloadProjects
    PopulateProjectListBox
    Projects_Loaded = True
  End If
    
  If (Contexts_Loaded = False) Or Update_Projects_And_Contexts_Each_Time Then
    DownloadContexts
    PopulateContextListBox
    Contexts_Loaded = True
  End If
End Sub
 
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
  ' Prevents use of the Close button, so we don't have to download the Projects and Contexts again
  TodoForm.Hide
  FormReset
  Cancel = True
End Sub