/HTCHome2.2/CalendarWidget/Calendar.xaml.cs

# · C# · 204 lines · 173 code · 24 blank · 7 comment · 19 complexity · 11b5ae3f98facfe52bbdcff98b27b8e7 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using E = HTCHome.Core.Environment;
  15. using L = HTCHome.Core.Logger;
  16. using System.Windows.Media.Animation;
  17. using Google.GData.Calendar;
  18. using System.Threading;
  19. using Google.GData.Client;
  20. namespace CalendarWidget
  21. {
  22. /// <summary>
  23. /// Interaction logic for UserControl1.xaml
  24. /// </summary>
  25. public partial class Calendar : UserControl
  26. {
  27. private Options options;
  28. private CalendarService service;
  29. public Calendar()
  30. {
  31. InitializeComponent();
  32. }
  33. private void Initialize()
  34. {
  35. Body.Source = new BitmapImage(new Uri(E.Path + "\\Calendar\\Resources\\body.png"));
  36. Header.Source = new BitmapImage(new Uri(E.Path + "\\Calendar\\Resources\\header.png"));
  37. SubHeader.Source = new BitmapImage(new Uri(E.Path + "\\Calendar\\Resources\\subheader.png"));
  38. Footer.Source = new BitmapImage(new Uri(E.Path + "\\Calendar\\Resources\\footer.png"));
  39. CalendarIcon.Source = new BitmapImage(new Uri(E.Path + "\\Calendar\\Resources\\calendar_icon.png"));
  40. }
  41. private void UserControl_Loaded(object sender, RoutedEventArgs e)
  42. {
  43. Initialize();
  44. HeaderTextBlock.Text = DateTime.Now.ToString("MMMM yyyy");
  45. CalendarIconDay.Text = DateTime.Now.Day.ToString();
  46. CalendarIconMonth.Text = DateTime.Now.ToString("MMM");
  47. BgRect.Opacity = Widget.Sett.opacity;
  48. Scale.ScaleX = Widget.Sett.scaleFactor;
  49. AgendaView.Date.MouseLeftButtonUp += new MouseButtonEventHandler(Header_MouseLeftButtonUp);
  50. AgendaView.AddNewItem += new CalendarWidget.AgendaView.AddNewItemEventHandler(AgendaView_AddNewItem);
  51. MenuItem optionsItem = new MenuItem();
  52. optionsItem.Header = Widget.LocaleManager.GetString("Options");
  53. optionsItem.Click += new RoutedEventHandler(optionsItem_Click);
  54. Widget.Parent.ContextMenu.Items.Insert(0, optionsItem);
  55. options = new Options();
  56. if (Widget.Sett.synchronizeWithGoogle)
  57. {
  58. ThreadStart threadStarter = delegate
  59. {
  60. L.Log("Calendar: Start Google calendar synchronization");
  61. service = CalendarHelper.GetService("HTC Home 2.0 Calendar Widget", Widget.Sett.username, Widget.Sett.password);
  62. L.Log("Calendar: Connected to Google calendar");
  63. IEnumerable<EventEntry> events = CalendarHelper.GetAllEvents(service, null);
  64. L.Log("Calendar: Events readed");
  65. if (events != null)
  66. {
  67. foreach (EventEntry ev in events)
  68. {
  69. /*var dates = from x in DayConverter.dict.Keys
  70. where ((x.Year == (ev.Times.First().StartTime.Date.Year) && (x.Month == (ev.Times.First().StartTime.Date.Month) && (x.Day == (ev.Times.First().StartTime.Date.Day)))))
  71. select x;
  72. var count = dates.Count();*/
  73. DateTime d = ev.Times.First().StartTime;
  74. if (DayConverter.dict.ContainsKey(ev.Times.First().StartTime))
  75. {
  76. CalendarEvent calendarEvent = new CalendarEvent();
  77. calendarEvent.Url = ev.EditUri.Content;
  78. calendarEvent.Title = ev.Title.Text;
  79. calendarEvent.StartTime = ev.Times.First().StartTime;
  80. calendarEvent.EndTime = ev.Times.First().EndTime;
  81. DayConverter.dict[ev.Times.First().StartTime] = calendarEvent;
  82. }
  83. else
  84. {
  85. CalendarEvent calendarEvent = new CalendarEvent();
  86. calendarEvent.Url = ev.EditUri.Content;
  87. calendarEvent.Title = ev.Title.Text;
  88. calendarEvent.StartTime = ev.Times.First().StartTime;
  89. calendarEvent.EndTime = ev.Times.First().EndTime;
  90. DayConverter.dict.Add(ev.Times.First().StartTime, calendarEvent);
  91. }
  92. CalendarControl.Dispatcher.Invoke((Action)delegate
  93. {
  94. CalendarControl.Cal.DisplayDate = DateTime.Now.AddMonths(-1);
  95. CalendarControl.Cal.DisplayDate = DateTime.Now;
  96. }, null);
  97. }
  98. }
  99. };
  100. Thread thread = new Thread(threadStarter);
  101. thread.SetApartmentState(ApartmentState.MTA);
  102. thread.Start();
  103. }
  104. }
  105. void optionsItem_Click(object sender, RoutedEventArgs e)
  106. {
  107. if (options.IsVisible)
  108. {
  109. options.Activate();
  110. return;
  111. }
  112. options = new Options();
  113. if (E.Locale == "he-IL" || E.Locale == "ar-SA")
  114. {
  115. options.FlowDirection = FlowDirection.RightToLeft;
  116. }
  117. else
  118. {
  119. options.FlowDirection = FlowDirection.LeftToRight;
  120. }
  121. options.ShowDialog();
  122. }
  123. void Header_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  124. {
  125. ((Storyboard)Resources["Swap2"]).Begin();
  126. }
  127. private void CalendarControl_CalendarDayChanged(DateTime? date)
  128. {
  129. AgendaView.ItemsPanel.Children.Clear();
  130. AgendaView.DateTime = date.Value;
  131. CalendarEvent value;
  132. var dates = from x in DayConverter.dict.Keys
  133. where ((x.Year == date.Value.Year) && (x.Month == date.Value.Month) && (x.Day == date.Value.Day))
  134. select x;
  135. if (dates.Count() > 0)
  136. {
  137. foreach (var d in dates)
  138. {
  139. if (DayConverter.dict.TryGetValue(d, out value))
  140. {
  141. AgendaItem item = new AgendaItem(d, AgendaView.ItemsPanel.Children.Count);
  142. item.Title.Text = value.Title;
  143. item.StartTime.Text = value.StartTime.ToShortTimeString();
  144. item.EndTime.Text = value.EndTime.ToShortTimeString();
  145. item.calendarEvent = value;
  146. item.service = service;
  147. AgendaView.ItemsPanel.Children.Add(item);
  148. }
  149. }
  150. }
  151. ((Storyboard)Resources["Swap1"]).Begin();
  152. AgendaView.Date.Text = date.Value.ToLongDateString();
  153. }
  154. void AgendaView_AddNewItem(DateTime d)
  155. {
  156. CalendarEvent ev = new CalendarEvent();
  157. ev.Title = Widget.LocaleManager.GetString("EditTitleTooltip");
  158. ev.StartTime = d.AddHours(DateTime.Now.Hour);
  159. ev.EndTime = d.AddHours(DateTime.Now.Hour + 1);
  160. AgendaItem item = new AgendaItem(d.AddSeconds(AgendaView.ItemsPanel.Children.Count), AgendaView.ItemsPanel.Children.Count);
  161. item.Title.Text = Widget.LocaleManager.GetString("EditTitleTooltip");
  162. item.StartTime.Text = ev.StartTime.ToShortTimeString();
  163. item.EndTime.Text = ev.EndTime.ToShortTimeString();
  164. AgendaView.ItemsPanel.Children.Add(item);
  165. if (Widget.Sett.synchronizeWithGoogle && service != null)
  166. {
  167. AtomEntry entry = CalendarHelper.AddEvent(service, ev.Title, "", "", d.AddHours(DateTime.Now.Hour), d.AddHours(DateTime.Now.Hour + 1));
  168. item.calendarEvent = ev;
  169. item.service = service;
  170. ev.Url = entry.EditUri.Content;
  171. }
  172. DayConverter.dict.Add(d.AddSeconds(AgendaView.ItemsPanel.Children.Count - 1), ev);
  173. }
  174. private void Storyboard_Completed(object sender, EventArgs e)
  175. {
  176. CalendarControl.Cal.DisplayDate = DateTime.Now.AddMonths(-1);
  177. CalendarControl.Cal.DisplayDate = DateTime.Now;
  178. }
  179. }
  180. }