/
SearchResults.xaml.cs
272 lines (241 loc) · 8.28 KB
/
SearchResults.xaml.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;
using Microsoft.Phone.Tasks;
namespace BugzillaApp
{
public partial class SearchResults : PhoneApplicationPage
{
delegate void ProcessZillaResult(JObject item);
public SearchResults()
{
InitializeComponent();
}
/// <summary>
/// Searching.....
/// </summary>
/// <param name="e"></param>
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
string query = "", fileName = "";
var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
EnsureDirectoryExists(store);
// IF we have a new query, check to see if it's still cached
if (NavigationContext.QueryString.TryGetValue("query", out query) && !string.IsNullOrEmpty(query))
{
string slug = query.Slugify();
fileName = string.Format("{0}{1}",
Settings.GetSettings().CachedQueriesFilePath, slug);
JObject cached = CheckCache(fileName);
if (cached != null)
{
ProcessBugZillaResults(cached);
}
else
{
PanoramaItem pItem = new PanoramaItem()
{
Name = slug,
Header = query,
Content = CreateProgressBar()
};
PanoView.Items.Add(pItem);
PanoView.DefaultItem = pItem;
QueryZilla(query, slug, fileName, ProcessBugZillaResults);
}
}
//
//Load cached searches
//
foreach (string file in store.GetFileNames(Settings.GetSettings().CachedQueriesFilePath + "*"))
{
var fullName = Settings.GetSettings().CachedQueriesFilePath + file;
if (fullName == fileName) continue;
using (var stream = store.OpenFile(fullName, FileMode.Open))
{
JObject jsonObj = ConvertJsonStreamToJObject(stream);
ProcessBugZillaResults(jsonObj);
}
}
}
/// <summary>
/// Process Json result from bugzilla API
/// </summary>
/// <param name="item"></param>
private void ProcessBugZillaResults(JObject item)
{
var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
PanoramaItem pItem = (PanoramaItem)PanoView.FindName(item["slug"].ToString());
if (pItem == null)
{
pItem = new PanoramaItem();
pItem.Name = item["slug"].ToString();
pItem.Header = item["query"].ToString();
PanoView.Items.Add(pItem);
}
pItem.Hold += new EventHandler<GestureEventArgs>((object sender, GestureEventArgs ge) =>
{
store.DeleteFile(item["filename"].ToString());
PanoView.Items.Remove(this); // is this enough for it to be garbage collected?
});
var lb = new ListBox();
var items = ((JArray)((JObject)item)["bugs"]).Select(bug => new BugzillaItem(bug));
foreach (var b in items)
{
ListBoxItem li = CreateBugzillaListItem(b);
lb.Items.Add(li);
}
pItem.Content = lb;
}
/// <summary>
/// Query Bugzilla, save data, and then call the process results callback
/// </summary>
/// <param name="query"></param>
/// <param name="slug"></param>
/// <param name="filename"></param>
/// <param name="callback"></param>
private void QueryZilla(string query, string slug, string filename, ProcessZillaResult callback)
{
var settings = Settings.GetSettings();
var bugzillaQuery = string.Format("https://{0}/{1}/bug?quicksearch={2}&cache_ok=1",
settings.BugzillaHost, settings.BugzillaApiVersion, query);
WebRequest req = HttpWebRequest.Create(bugzillaQuery);
req.BeginGetResponse(new AsyncCallback((IAsyncResult resResult) =>
{
HttpWebResponse response = (HttpWebResponse)((HttpWebRequest)resResult.AsyncState).EndGetResponse(resResult);
JObject jsonObj = null;
using (Stream streamResponse = response.GetResponseStream())
{
jsonObj = ConvertJsonStreamToJObject(streamResponse);
jsonObj["query"] = query;
jsonObj["slug"] = slug;
jsonObj["filename"] = filename;
JsonSerializer serializer = new JsonSerializer();
var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
var fileStream = store.CreateFile(filename);
StreamWriter sr = new StreamWriter(fileStream);
serializer.Serialize(sr, jsonObj);
sr.Close();
}
response.Close();
Dispatcher.BeginInvoke(callback, jsonObj);
}), req);
}
/// <summary>
/// Convert json stream to a JObject
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private JObject ConvertJsonStreamToJObject(Stream s)
{
JsonSerializer serializer = new JsonSerializer();
StreamReader streamRead = new StreamReader(s);
var r = new JsonTextReader(streamRead);
var result = (JObject)serializer.Deserialize(r);
return result;
}
/// <summary>
/// Check to see if the results of this query already exist in local storage.
/// If so return the deserialize object
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private JObject CheckCache(string fileName)
{
JObject result = null;
var store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
if (store.FileExists(fileName))
{
if (store.GetLastWriteTime(fileName) > DateTime.Now.AddMinutes(10))
{
store.DeleteFile(fileName);
}
else
{
using (var stream = store.OpenFile(fileName, FileMode.Open))
{
result = ConvertJsonStreamToJObject(stream);
}
}
}
return result;
}
/// <summary>
/// Create UI listitems for BugzillaItems
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
private static ListBoxItem CreateBugzillaListItem(BugzillaItem b)
{
ListBoxItem li = new ListBoxItem();
li.DoubleTap += new EventHandler<GestureEventArgs>((object sender, GestureEventArgs ge) =>
{
WebBrowserTask task = new WebBrowserTask();
task.Uri = new Uri("https://bugzilla.mozilla.org/show_bug.cgi?id=" + b.ID.ToString());
task.Show();
});
StackPanel s = new StackPanel();
s.Children.Add(new TextBlock()
{
Text = string.Format("[{1}] {0}", b.Summary, b.ID),
TextWrapping = TextWrapping.Wrap
});
s.Children.Add(new TextBlock()
{
Text = string.Format("{0} - {1} - {2}", b.Product, b.Component, b.AssignedTo)
});
s.Children.Add(new TextBlock()
{
Text = string.Format("{0:MMM d yyyy} - {1} - {2}", b.CreatedDate, b.Status, b.Resolution),
Margin = new Thickness(0, 0, 0, 10)
});
li.Content = s;
return li;
}
/// <summary>
/// Returns a progress bar
/// </summary>
/// <returns></returns>
private StackPanel CreateProgressBar()
{
var stack = new StackPanel();
var pb = new ProgressBar()
{
Margin = new Thickness(0, 120, 0, 50),
Opacity = 1,
Height = 10,
VerticalAlignment = System.Windows.VerticalAlignment.Center,
Name = "progressBar",
Style = this.Resources["PerformanceProgressBar"] as Style,
IsIndeterminate = true,
Visibility = System.Windows.Visibility.Visible
};
stack.Children.Add(pb);
return stack;
}
/// <summary>
/// Make sure the cached queries directory exists
/// </summary>
/// <param name="store"></param>
private static void EnsureDirectoryExists(System.IO.IsolatedStorage.IsolatedStorageFile store)
{
if (!store.DirectoryExists(Settings.GetSettings().CachedQueriesFilePath))
{
store.CreateDirectory(Settings.GetSettings().CachedQueriesFilePath.Replace("/", ""));
}
}
}
}