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
|
/***************************************************************************
* Feed.cs
*
* Copyright (C) 2007 Michael C. Urbanski
* Written by Mike Urbanski <michael.c.urbanski@gmail.com>
****************************************************************************/
/* THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW:
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Mono.Unix;
using Hyena;
using Hyena.Data.Sqlite;
using Migo.Net;
using Migo.TaskCore;
using Migo.DownloadCore;
namespace Migo.Syndication
{
public enum FeedAutoDownload : int
{
All = 0,
One = 1,
None = 2
}
// TODO remove this, way too redundant with DownloadStatus
public enum PodcastFeedActivity : int {
Updating = 0,
UpdatePending = 1,
UpdateFailed = 2,
ItemsDownloading = 4,
ItemsQueued = 5,
None = 6
}
public class FeedProvider : MigoModelProvider<Feed>
{
public FeedProvider (HyenaSqliteConnection connection) : base (connection, "PodcastSyndications")
{
}
protected override void CreateTable ()
{
base.CreateTable ();
CreateIndex ("PodcastSyndicationsIndex", "IsSubscribed, Title");
}
protected override int ModelVersion {
get { return 4; }
}
protected override void MigrateTable (int old_version)
{
CheckTable ();
if (old_version < 2) {
Connection.Execute (String.Format ("UPDATE {0} SET IsSubscribed=1", TableName));
}
if (old_version < 3) {
CreateIndex ("PodcastSyndicationsIndex", "IsSubscribed, Title");
}
if (old_version < 4) {
Connection.Execute (String.Format ("UPDATE {0} SET MaxItemCount=0 WHERE MaxItemCount=200", TableName));
}
}
}
public class Feed : MigoItem<Feed>
{
private static FeedProvider provider;
public static FeedProvider Provider {
get { return provider; }
}
public static void Init () {
provider = new FeedProvider (FeedsManager.Instance.Connection);
}
public static bool Exists (string url)
{
return Provider.Connection.Query<int> (String.Format ("select count(*) from {0} where url = ?", Provider.TableName), url) != 0;
}
//private bool canceled;
//private bool deleted;
//private bool updating;
//private ManualResetEvent updatingHandle = new ManualResetEvent (true);
private readonly object sync = new object ();
private string copyright;
private string description;
private string image_url;
private int update_period_minutes = 24 * 60;
private string language;
private DateTime last_build_date = DateTime.MinValue;
private FeedDownloadError lastDownloadError;
private DateTime last_download_time = DateTime.MinValue;
private string link;
//private string local_enclosure_path;
private long dbid = -1;
private long maxItemCount = 0;
private DateTime pubDate;
private FeedSyncSetting syncSetting;
private string title;
private string url;
private string keywords, category;
#region Database-bound Properties
[DatabaseColumn ("FeedID", Constraints = DatabaseColumnConstraints.PrimaryKey)]
public override long DbId {
get { return dbid; }
protected set { dbid = value; }
}
public static string UnknownPodcastTitle = Catalog.GetString ("Unknown Podcast");
[DatabaseColumn]
public string Title {
get { return title ?? UnknownPodcastTitle; }
set { title = value; }
}
[DatabaseColumn]
public string Description {
get { return description; }
set { description = value; }
}
[DatabaseColumn]
public string Url {
get { return url; }
set { url = value; }
}
[DatabaseColumn]
public string Keywords {
get { return keywords; }
set { keywords = value; }
}
[DatabaseColumn]
public string Category {
get { return category; }
set { category = value; }
}
[DatabaseColumn]
public string Copyright {
get { return copyright; }
set { copyright = value; }
}
[DatabaseColumn]
public string ImageUrl {
get { return image_url; }
set { image_url = value; }
}
[DatabaseColumn]
public int UpdatePeriodMinutes {
get { return update_period_minutes; }
set { update_period_minutes = value; }
}
[DatabaseColumn]
public string Language {
get { return language; }
set { language = value; }
}
[DatabaseColumn]
public FeedDownloadError LastDownloadError {
get { return lastDownloadError; }
set { lastDownloadError = value; }
}
[DatabaseColumn]
public DateTime LastDownloadTime {
get { return last_download_time; }
set { last_download_time = value; }
}
[DatabaseColumn]
public string Link {
get { return link; }
set { link = value; }
}
//[DatabaseColumn]
public string LocalEnclosurePath {
get {
string escaped = Hyena.StringUtil.EscapeFilename (Title);
return Path.Combine (FeedsManager.Instance.PodcastStorageDirectory, escaped);
}
//set { local_enclosure_path = value; }
}
[DatabaseColumn]
public long MaxItemCount {
get { return maxItemCount; }
set { maxItemCount = value; }
}
[DatabaseColumn]
public DateTime PubDate {
get { return pubDate; }
set { pubDate = value; }
}
[DatabaseColumn]
public DateTime LastBuildDate {
get { return last_build_date; }
set { last_build_date = value; }
}
/*private DateTime last_downloaded;
[DatabaseColumn]
public DateTime LastDownloaded {
get { return last_downloaded; }
set { last_downloaded = value; }
}*/
[DatabaseColumn]
public FeedSyncSetting SyncSetting {
get { return syncSetting; }
set { syncSetting = value; }
}
[DatabaseColumn]
protected DateTime last_auto_download = DateTime.MinValue;
public DateTime LastAutoDownload {
get { return last_auto_download; }
set { last_auto_download = value; }
}
[DatabaseColumn("AutoDownload")]
protected FeedAutoDownload auto_download = FeedAutoDownload.None;
public FeedAutoDownload AutoDownload {
get { return auto_download; }
set {
if (value == auto_download)
return;
auto_download = value;
CheckForItemsToDownload ();
}
}
[DatabaseColumn("DownloadStatus")]
private FeedDownloadStatus download_status;
public FeedDownloadStatus DownloadStatus {
get { return download_status; }
set { download_status = value; }
}
[DatabaseColumn("IsSubscribed")]
private bool is_subscribed;
public bool IsSubscribed {
get { return is_subscribed; }
set { is_subscribed = value; }
}
#endregion
#region Other Properties
// TODO remove this, way too redundant with DownloadStatus
/*public PodcastFeedActivity Activity {
get { return activity; }
PodcastFeedActivity ret = PodcastFeedActivity.None;
if (this == All) {
return ret;
}
switch (DownloadStatus) {
case FeedDownloadStatus.Pending:
ret = PodcastFeedActivity.UpdatePending;
break;
case FeedDownloadStatus.Downloading:
ret = PodcastFeedActivity.Updating;
break;
case FeedDownloadStatus.DownloadFailed:
ret = PodcastFeedActivity.UpdateFailed;
break;
}
if (ret != PodcastFeedActivity.Updating) {
if (ActiveDownloadCount > 0) {
ret = PodcastFeedActivity.ItemsDownloading;
} else if (QueuedDownloadCount > 0) {
ret = PodcastFeedActivity.ItemsQueued;
}
}
return ret;
}
}*/
public IEnumerable<FeedItem> Items {
get {
if (DbId > 0) {
foreach (FeedItem item in
FeedItem.Provider.FetchAllMatching (String.Format ("{0}.FeedID = {1} ORDER BY {0}.PubDate DESC", FeedItem.Provider.TableName, DbId)))
{
yield return item;
}
}
}
}
#endregion
private static FeedManager Manager {
get { return FeedsManager.Instance.FeedManager; }
}
#region Constructors
public Feed (string url, FeedAutoDownload auto_download) : this ()
{
Url = url;
this.auto_download = auto_download;
}
public Feed ()
{
}
#endregion
#region Internal Methods
// Removing a FeedItem means removing the downloaded file.
/*public void Remove (FeedItem item)
{
if (item == null) {
throw new ArgumentNullException ("item");
}
if (items.Remove (item)) {
inactive_items.Add (item);
OnFeedItemRemoved (item);
}
}
}*/
/*public void Remove (IEnumerable<FeedItem> itms)
{
if (removedItems.Count > 0) {
OnItemsChanged ();
}
}
}*/
#endregion
#region Private Methods
public void SetItems (IEnumerable<FeedItem> items)
{
bool added_any = false;
foreach (FeedItem item in items) {
added_any |= AddItem (item);
}
if (added_any) {
CheckForItemsToArchive ();
Manager.OnFeedsChanged ();
CheckForItemsToDownload ();
}
}
private bool AddItem (FeedItem item)
{
try {
if (!FeedItem.Exists (this.DbId, item.Guid)) {
item.Feed = this;
item.Save ();
return true;
}
} catch (Exception e) {
Hyena.Log.Exception (e);
}
return false;
}
/*private void UpdateItems (IEnumerable<FeedItem> new_items)
{
ICollection<FeedItem> tmpNew = null;
List<FeedItem> zombies = new List<FeedItem> ();
if (items.Count == 0 && inactive_items.Count == 0) {
tmpNew = new List<FeedItem> (new_items);
} else {
// Get remote items that aren't in the items list
tmpNew = Diff (items, new_items);
// Of those, remove the ones that are in our inactive list
tmpNew = Diff (inactive_items, tmpNew);
// Get a list of inactive items that aren't in the remote list any longer
ICollection<FeedItem> doubleKilledZombies = Diff (
new_items, inactive_items
);
foreach (FeedItem zombie in doubleKilledZombies) {
inactive_items.Remove (zombie);
}
zombies.AddRange (doubleKilledZombies);
foreach (FeedItem fi in Diff (new_items, items)) {
if (fi.Enclosure != null &&
!String.IsNullOrEmpty (fi.Enclosure.LocalPath)) {
// A hack for the podcast plugin, keeps downloaded items
// from being deleted when they are no longer in the feed.
continue;
}
zombies.Add (fi);
}
}
if (tmpNew.Count > 0) {
Add (tmpNew);
}
// TODO merge...should we really be deleting these items?
if (zombies.Count > 0) {
foreach (FeedItem item in zombies) {
if (item.Active) {
zombie.Delete ();
}
}
// TODO merge
//ItemsTableManager.Delete (zombies);
}
}
// Written before LINQ, will update.
private ICollection<FeedItem> Diff (IEnumerable<FeedItem> baseSet,
IEnumerable<FeedItem> overlay) {
bool found;
List<FeedItem> diff = new List<FeedItem> ();
foreach (FeedItem opi in overlay) {
found = false;
foreach (FeedItem bpi in baseSet) {
if (opi.Title == bpi.Title &&
opi.Description == bpi.Description) {
found = true;
break;
}
}
if (!found) {
diff.Add (opi);
}
}
return diff;
}*/
#endregion
#region Public Methods
public void Update ()
{
Manager.QueueUpdate (this);
}
public void Delete ()
{
Delete (true);
Manager.OnFeedsChanged ();
}
public void Delete (bool deleteEnclosures)
{
lock (sync) {
//if (deleted)
// return;
//if (updating) {
// Manager.CancelUpdate (this);
//}
foreach (FeedItem item in Items) {
item.Delete (deleteEnclosures);
}
Provider.Delete (this);
}
//updatingHandle.WaitOne ();
Manager.OnFeedsChanged ();
}
public void MarkAllItemsRead ()
{
lock (sync) {
foreach (FeedItem i in Items) {
i.IsRead = true;
}
}
}
public override string ToString ()
{
return String.Format ("Title: {0} - Url: {1}", Title, Url);
}
public void Save ()
{
Save (true);
}
public void Save (bool notify)
{
Provider.Save (this);
CheckForItemsToArchive ();
if (LastBuildDate > LastAutoDownload) {
CheckForItemsToDownload ();
}
if (notify) {
Manager.OnFeedsChanged ();
}
}
private void CheckForItemsToArchive ()
{
if (MaxItemCount == 0)
return;
int i = 0;
foreach (var item in Items) {
if (!item.IsRead) {
if (i++ >= MaxItemCount) {
item.IsRead = true;
item.Save (false);
}
}
}
}
private void CheckForItemsToDownload ()
{
if (LastDownloadError != FeedDownloadError.None || AutoDownload == FeedAutoDownload.None)
return;
bool only_first = (AutoDownload == FeedAutoDownload.One);
bool any = false;
foreach (FeedItem item in Items) {
if (item.Enclosure != null && item.Active &&
item.Enclosure.DownloadStatus != FeedDownloadStatus.Downloaded && item.PubDate > LastAutoDownload)
{
item.Enclosure.AsyncDownload ();
any = true;
if (only_first)
break;
}
}
if (any) {
LastAutoDownload = DateTime.Now;
Save ();
}
}
/*private bool SetCanceled ()
{
bool ret = false;
if (!canceled && updating) {
ret = canceled = true;
}
return ret;
}*/
#endregion
}
}
|