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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using Microsoft.Win32;
using Google.GData.YouTube;
using Google.GData.Client;
using Google.YouTube;
using NotifierForYT.Properties;
namespace NotifierForYT
{
public partial class YouTubeNotifier : Form
{
private NotifyIcon nIcon;
private string authToken;
private YouTubeRequest ytRequest;
private string user;
private int initialPullinHours;
private int updateFrequency;
private int notificationDuration;
private bool closeThis=false;
private DateTime lastUpdate;
private const string AppKey = "NotifierYT";
private const string YTNOTIFIERKEY = "Software\\GoogleYouTubeNotifier";
private const string YTDEVKEY = "AI39si4v3E6oIYiI60ndCNDqnPP5lCqO28DSvvDPnQt-Mqia5uPz2e4E-gMSBVwHXwyn_LF1tWox4LyM-0YQd2o4i_3GcXxa2Q";
private List<Activity> allActivities = new List<Activity>();
private List<Video> allVideos = new List<Video>();
public YouTubeNotifier()
{
InitializeComponent();
CreateNotifyIcon();
this.Icon = this.nIcon.Icon;
if (CheckForFirstStartup() == true)
{
ShowSettings();
}
else
{
if (this.authToken != null)
{
this.isAuthenticated.Checked = true;
UpdateActivities();
}
}
this.Text = AppTitle;
this.refreshTimer.Interval = 10000;
this.refreshTimer.Enabled = true;
}
private void ShowSettings()
{
ShowMe(this.tabPageSettings);
}
private void CreateNotifyIcon()
{
this.ShowInTaskbar = false;
this.nIcon = new NotifyIcon(this.components);
// Displays a TV Icon
// Bitmap b = Resources.gdata_youtube;
// this.nIcon.Icon = Icon.FromHandle(b.GetHicon());
Bitmap b = Resources.ytfavicon;
IntPtr Hicon = b.GetHicon();
this.nIcon.Icon = Icon.FromHandle(Hicon);
this.nIcon.Text = AppTitle + " - double click to restore it";
this.nIcon.Visible = true;
this.nIcon.ContextMenuStrip = this.defaultMenu;
this.nIcon.DoubleClick += new EventHandler(nIcon_DoubleClick);
HideMe();
}
private void nIcon_DoubleClick(Object sender, EventArgs e)
{
ToggleVisibility();
}
private bool CheckForFirstStartup()
{
RegistryKey ytNotifier = Registry.CurrentUser.OpenSubKey(YTNOTIFIERKEY);
if (ytNotifier == null)
return true;
this.user = this.userName.Text = ytNotifier.GetValue("userName") as string;
this.authToken = ytNotifier.GetValue("token") as string;
this.InitialDataPullTime.Value = this.initialPullinHours = (int)ytNotifier.GetValue("initialDataPullTime", 24);
this.UpdateFrequency.Value = this.updateFrequency = (int)ytNotifier.GetValue("updateFrequency", 15);
this.notifcationBalloons.Value = this.notificationDuration = (int)ytNotifier.GetValue("notificationDuration", 10);
string s = ytNotifier.GetValue("userList") as string;
if (s != null)
{
string[] users = s.Split(',');
foreach (string u in users)
{
this.usernameGrid.Rows.Add(u);
}
}
YouTubeRequestSettings settings = new YouTubeRequestSettings(AppKey,
YTDEVKEY,
this.userName.Text,
"");
this.ytRequest = new YouTubeRequest(settings);
OnAuthenticationModified(this.authToken);
return false;
}
private void OnAuthenticationModified(string token)
{
this.authToken = token;
this.isAuthenticated.Checked = token != null;
if (this.ytRequest != null && this.ytRequest.Service != null)
this.ytRequest.Service.SetAuthenticationToken(token);
}
private void ButtonSaveSettings_Click(object sender, EventArgs e)
{
bool fHide = true;
if (this.isAuthenticated.Checked == false && this.userName.Text.Length > 0)
{
// let's see if we get a valid authtoken back for the passed in credentials....
YouTubeRequestSettings settings = new YouTubeRequestSettings(AppKey,
YTDEVKEY,
this.userName.Text,
this.passWord.Text);
// settings.PageSize = 15;
this.ytRequest = new YouTubeRequest(settings);
try
{
this.authToken = this.ytRequest.Service.QueryClientLoginToken();
this.passWord.Text = "";
}
catch
{
MessageBox.Show("There was a problem with your credentials");
this.authToken = null;
fHide = false;
}
OnAuthenticationModified(this.authToken);
}
// let's save the username to the registry, but not the password
RegistryKey ytNotifier = Registry.CurrentUser.OpenSubKey(YTNOTIFIERKEY, true);
if (ytNotifier == null)
{
ytNotifier = Registry.CurrentUser.CreateSubKey(YTNOTIFIERKEY);
}
ytNotifier.SetValue("initialDataPullTime", this.InitialDataPullTime.Value, RegistryValueKind.DWord);
ytNotifier.SetValue("updateFrequency", this.UpdateFrequency.Value, RegistryValueKind.DWord);
ytNotifier.SetValue("notificationDuration", this.notifcationBalloons.Value, RegistryValueKind.DWord);
ytNotifier.SetValue("userList", GetUserNamesToSave());
this.initialPullinHours = (int)this.InitialDataPullTime.Value;
this.updateFrequency = (int)this.UpdateFrequency.Value;
this.notificationDuration = (int)this.notifcationBalloons.Value;
if (this.authToken != null)
{
ytNotifier.SetValue("userName", this.userName.Text);
ytNotifier.SetValue("token", this.authToken);
this.user = this.userName.Text;
}
if (fHide == true)
{
HideMe();
UpdateActivities();
}
}
private void userName_TextChanged(object sender, EventArgs e)
{
this.isAuthenticated.Checked = false;
}
private void passWord_TextChanged(object sender, EventArgs e)
{
this.isAuthenticated.Checked = false;
}
private void ButtonIgnore_Click(object sender, EventArgs e)
{
HideMe();
this.userName.Text = this.user;
this.passWord.Text = null;
this.UpdateFrequency.Value = this.updateFrequency;
this.InitialDataPullTime.Value = this.initialPullinHours;
this.notifcationBalloons.Value = this.notificationDuration;
}
private void refreshTimer_Tick(object sender, EventArgs e)
{
CheckForUpdateActivities();
}
private void CheckForUpdateActivities()
{
DateTime d = this.lastUpdate.AddMinutes(this.updateFrequency);
if (d < DateTime.Now)
{
UpdateActivities();
}
}
private List<string> GetUserNames()
{
// first update the not authenticated case.
List<string> users = new List<string>();
foreach (DataGridViewRow r in this.usernameGrid.Rows)
{
if (r.Cells[0].Value != null)
users.Add(r.Cells[0].Value as string);
}
return users;
}
private string GetUserNamesToSave()
{
string ret = "";
List<string> users = GetUserNames();
foreach (string s in users)
{
if (String.IsNullOrEmpty(s) != true)
{
if (ret.Length > 0)
{
ret += ",";
}
ret += s;
}
}
return ret;
}
private void UpdateActivities()
{
this.refreshTimer.Enabled = false;
DateTime since = DateTime.MinValue;
if (this.allActivities.Count == 0)
{
// first call, do a modified-since query
since = DateTime.Now.AddHours(-1 * this.initialPullinHours);
}
int iCounter = 0;
this.nIcon.BalloonTipText = "";
List<string> users = GetUserNames();
if (users.Count > 0 && String.IsNullOrEmpty(users[0]) != true)
{
// let's see if we get a valid authtoken back for the passed in credentials....
YouTubeRequestSettings settings = new YouTubeRequestSettings("YouTubeNotifier",
YTDEVKEY);
// settings.PageSize = 15;
YouTubeRequest r = new YouTubeRequest(settings);
Feed<Activity> pf = r.GetActivities(users, since);
iCounter += ProcessFeed(pf, since);
}
if (this.authToken != null)
{
Feed<Activity> f = this.ytRequest.GetActivities(since);
iCounter += ProcessFeed(f, since);
}
// now redo the layout in the right order of controls:
this.linkList.SuspendLayout();
this.linkList.Controls.Clear();
foreach (Activity act in this.allActivities)
{
LinkLabel l = new LinkLabel();
string when = act.Updated.ToShortDateString();
if (act.Updated.Date == DateTime.Now.Date)
{
// it happened today
when = act.Updated.ToShortTimeString();
}
if (act.Updated.Date == DateTime.Now.AddDays(-1).Date)
{
when = "yesterday, at " + act.Updated.ToShortTimeString();
}
l.Text = act.Author + " has ";
bool noLink = false;
int len = 5;
switch (act.Type)
{
case ActivityType.Commented:
l.Text += "commented on ";
len = AddVideoText(l, act.VideoId);
break;
case ActivityType.Favorited:
l.Text += "favorited ";
len = AddVideoText(l, act.VideoId);
break;
case ActivityType.FriendAdded:
l.Text += "added a friend - " + act.Username ;
noLink = true;
break;
case ActivityType.Rated:
l.Text += "rated ";
len = AddVideoText(l, act.VideoId);
break;
case ActivityType.Shared:
l.Text += "shared ";
len = AddVideoText(l, act.VideoId);
break;
case ActivityType.SubscriptionAdded:
l.Text += "subscriped to " + act.Username;
noLink = true;
break;
case ActivityType.Uploaded:
l.Text += "uploaded ";
len = AddVideoText(l, act.VideoId);
break;
}
l.AutoSize = true;
if (noLink == false)
{
l.Links[0].Start = l.Text.Length - len;
l.Links[0].Length = len;
l.Links[0].LinkData = YouTubeQuery.CreateVideoWatchUri(act.VideoId);
l.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.Label_LinkClicked);
}
else
{
// create a link for the user
l.Links[0].Start = l.Text.Length - act.Username.Length;
l.Links[0].Length = act.Username.Length;
l.Links[0].LinkData = "https://www.youtube.com/user/" + act.Username;
l.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.Label_LinkClicked);
}
l.Text += " (" + when + ")";
this.linkList.Controls.Add(l);
}
this.linkList.ResumeLayout();
if (iCounter > 0)
{
this.nIcon.BalloonTipTitle = "YouTube activities for " + this.userName.Text + " friends";
this.nIcon.ShowBalloonTip(this.notificationDuration * 1000);
}
UpdateTitle();
this.refreshTimer.Enabled = true;
this.lastUpdate = DateTime.Now;
}
private int AddVideoText(LinkLabel l, string videoId)
{
int len = 5;
Video v = FindVideo(videoId);
if (v != null)
{
l.Text += v.Title;
len = v.Title.Length;
}
else
{
l.Text += " a video";
}
return len;
}
private void UpdateTitle()
{
this.nIcon.Text = AppTitle + " " + this.UnreadCount + " unread activities";
}
private int ProcessFeed(Feed<Activity> feed, DateTime since)
{
List<Activity> newGuys = new List<Activity>();
try
{
foreach (Activity a in feed.Entries)
{
// first check if the activity is inside our maximum timeframe
// substract the max number of hours to pull
DateTime oldest = DateTime.Now.AddHours(-1 * this.initialPullinHours);
if (a.Updated > oldest)
{
// so it's part of the timeline. Let's see if we already have it
if (IsContained(a) == false)
{
AddBallonText(newGuys.Count, a.ToString());
if (since == DateTime.MinValue)
{
this.allActivities.Insert(0, a);
}
else
{
this.allActivities.Add(a);
}
newGuys.Add(a);
}
}
}
}
catch (GDataForbiddenException e)
{
MessageBox.Show("Your accesstoken has expired.", "Please reauthenticate by providing your password again");
OnAuthenticationModified(null);
ShowSettings();
}
catch (InvalidCredentialsException e)
{
MessageBox.Show("Your accesstoken has expired.", "Please reauthenticate by providing your password again");
OnAuthenticationModified(null);
ShowSettings();
}
catch (GDataRequestException e)
{
HttpWebResponse r = e.Response as HttpWebResponse;
if (r != null && r.StatusCode == HttpStatusCode.Unauthorized)
{
MessageBox.Show("Your accesstoken has expired.", "Please reauthenticate by providing your password again");
OnAuthenticationModified(null);
ShowSettings();
}
else if (r != null && r.StatusCode == HttpStatusCode.NotModified)
{
// nothing new there, so ignore the exception.
}
else
MessageBox.Show(e.ResponseString, "There was a problem getting the data from YouTube");
}
// now get the metadata for the new guys
GetMetaData(newGuys);
return newGuys.Count;
}
private void GetMetaData(List<Activity> list)
{
Feed<Video> meta = this.ytRequest.GetVideoMetaData(list);
foreach (Video v in meta.Entries)
{
if (IsContained(v) == false)
{
this.allVideos.Add(v);
}
}
}
private int UnreadCount
{
get
{
int iCounter = 0;
foreach (LinkLabel l in linkList.Controls)
{
if (l.Links[0].Visited == false)
iCounter++;
}
return iCounter;
}
}
private void AddBallonText(int iCounter, string text)
{
if (iCounter == 0)
this.nIcon.BalloonTipText = text;
else if (iCounter < 4)
this.nIcon.BalloonTipText += "\r" + text;
else if (iCounter == 4)
this.nIcon.BalloonTipText += "\r" + "and more...";
}
private bool IsContained(Activity a)
{
foreach (Activity act in this.allActivities)
{
if (act.Id == a.Id)
return true;
}
return false;
}
private bool IsContained(Video v)
{
foreach (Video video in this.allVideos)
{
if (video.Id == v.Id)
return true;
}
return false;
}
private Video FindVideo(string videoId)
{
foreach (Video video in this.allVideos)
{
if (video.VideoId == videoId)
return video;
}
return null;
}
private void HideMe()
{
this.Hide();
this.WindowState = FormWindowState.Minimized;
}
private void ToggleVisibility()
{
if (this.WindowState == FormWindowState.Minimized)
{
ShowMe(null);
}
else
{
HideMe();
}
}
private void ShowMe(TabPage page)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
if (page == null)
this.tabControl.SelectedTab = this.tabPageActivities;
else
this.tabControl.SelectedTab = page;
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowSettings();
}
private void activitiesToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowMe(this.tabPageActivities);
}
private void YouTubeNotifier_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing && this.closeThis == false)
{
e.Cancel = true;
HideMe();
this.nIcon.BalloonTipText = "Use the context menu to exit or restore particular parts of the application";
this.nIcon.BalloonTipTitle = AppTitle + " is still running !";
this.nIcon.ShowBalloonTip(this.notificationDuration * 1000);
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.closeThis = true;
this.Close();
}
private void clearActivitiesToolStripMenuItem_Click(object sender, EventArgs e)
{
this.linkList.Controls.Clear();
UpdateTitle();
}
private void YouTubeNotifier_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
HideMe();
}
private void Label_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
string u = e.Link.LinkData as string;
if (u != null)
{
try
{
System.Diagnostics.Process.Start(u);
}
catch
{
MessageBox.Show("There was a problem navigating to: " + u);
}
e.Link.Visited = true;
}
UpdateTitle();
}
private void button1_Click(object sender, EventArgs e)
{
// let's save the username to the registry, but not the password
RegistryKey ytNotifier = Registry.CurrentUser.OpenSubKey(YTNOTIFIERKEY, true);
if (ytNotifier == null)
{
ytNotifier = Registry.CurrentUser.CreateSubKey(YTNOTIFIERKEY);
}
ytNotifier.SetValue("userList", GetUserNamesToSave());
HideMe();
UpdateActivities();
}
private void usersToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowMe(this.tabPageUsers);
}
private void usernameGrid_UserAddedRow(object sender, DataGridViewRowEventArgs e)
{
this.usernameGrid.AllowUserToAddRows = true;
if (this.usernameGrid.Rows.Count >= 20)
{
this.usernameGrid.AllowUserToAddRows = false;
}
}
}
}
|