forked from ViennaRSS/vienna-rss
/
Import.m
133 lines (118 loc) · 4.73 KB
/
Import.m
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
//
// Import.m
// Vienna
//
// Created by Steve on 5/27/05.
// Copyright (c) 2004-2005 Steve Palmer. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "Import.h"
#import "StringExtensions.h"
#import "BJRWindowWithToolbar.h"
#import "Database.h"
@implementation Import
/* importFromFile
* Import a list of RSS subscriptions.
*/
+ (void)importFromFile:(NSString *)importFileName
{
NSData * data = [NSData dataWithContentsOfFile:importFileName.stringByExpandingTildeInPath];
BOOL hasError = NO;
NSInteger countImported = 0;
if (data != nil)
{
NSError *error = nil;
NSXMLDocument *opmlDocument = [[NSXMLDocument alloc] initWithData:data
options:NSXMLNodeOptionsNone|NSXMLNodeLoadExternalEntitiesNever
error:&error];
if (error)
{
NSRunAlertPanel(NSLocalizedString(@"Error importing subscriptions title", nil),
NSLocalizedString(@"Error importing subscriptions body", nil),
NSLocalizedString(@"OK", nil), nil, nil);
hasError = YES;
}
else
{
NSArray *outlines = [opmlDocument nodesForXPath:@"opml/body/outline" error:nil];
countImported = [self importSubscriptionGroup:outlines underParent:MA_Root_Folder];
}
}
// Announce how many we successfully imported
if (!hasError)
{
NSRunAlertPanel(NSLocalizedString(@"RSS Subscription Import Title", nil), NSLocalizedString(@"%d subscriptions successfully imported", nil), NSLocalizedString(@"OK", nil), nil, nil, countImported);
}
}
/* importSubscriptionGroup
* Import one group of an OPML subscription tree.
*/
+ (NSInteger)importSubscriptionGroup:(NSArray *)outlines underParent:(NSInteger)parentId
{
NSInteger countImported = 0;
for (NSXMLElement *outlineElement in outlines)
{
NSString *feedText = ([outlineElement attributeForName:@"text"].stringValue).stringByEscapingExtendedCharacters;
NSString *feedDescription = ([outlineElement attributeForName:@"description"].stringValue).stringByEscapingExtendedCharacters;
NSString *feedURL = ([outlineElement attributeForName:@"xmlUrl"].stringValue).stringByEscapingExtendedCharacters;
NSString *feedHomePage = ([outlineElement attributeForName:@"htmlUrl"].stringValue).stringByEscapingExtendedCharacters;
Database * dbManager = [Database sharedManager];
// Some OPML exports use 'title' instead of 'text'.
if (feedText == nil || feedText.length == 0u)
{
NSString * feedTitle = ([outlineElement attributeForName:@"title"].stringValue).stringByEscapingExtendedCharacters;
if (feedTitle != nil) {
feedText = feedTitle;
}
}
// Do double-decoding of the title to get around a bug in some commercial newsreaders
// where they double-encode characters
feedText = feedText.stringByUnescapingExtendedCharacters;
if (feedURL == nil)
{
// This is a new group so try to create it. If there's an error then default to adding
// the sub-group items under the parent.
if (feedText != nil)
{
NSInteger folderId = [dbManager addFolder:parentId afterChild:-1
folderName:feedText type:MA_Group_Folder
canAppendIndex:NO];
if (folderId == -1) {
folderId = MA_Root_Folder;
}
countImported += [self importSubscriptionGroup:outlineElement.children underParent:folderId];
}
}
else if (feedText != nil)
{
Folder * folder;
NSInteger folderId;
if ((folder = [dbManager folderFromFeedURL:feedURL]) != nil)
folderId = folder.itemId;
else
{
folderId = [dbManager addRSSFolder:feedText underParent:parentId afterChild:-1 subscriptionURL:feedURL];
++countImported;
}
if (feedDescription != nil) {
[dbManager setDescription:feedDescription forFolder:folderId];
}
if (feedHomePage != nil) {
[dbManager setHomePage:feedHomePage forFolder:folderId];
}
}
}
return countImported;
}
@end