File: RSSArticle%2BStorage.m

package info (click to toggle)
rsskit 0.3-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 656 kB
  • ctags: 572
  • sloc: objc: 2,041; makefile: 48; sh: 4
file content (178 lines) | stat: -rw-r--r-- 5,268 bytes parent folder | download | duplicates (2)
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
/*  -*-objc-*-
 *
 *  GNUstep RSS Kit
 *  Copyright (C) 2006 Guenther Noack
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation, in version 2.1
 *  of the License
 * 
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#import "RSSLinks.h"
#import "RSSFactory.h"
#import "RSSArticle+Storage.h"
#import <Foundation/Foundation.h>
#import "GNUstep.h"



/*
 * -----------------------------------------------------
 * RSSLink helper methods to serialize this into a Plist
 * -----------------------------------------------------
 */

@interface RSSLink (Storage)
-(NSDictionary*) plistDictionary;
+(id) urlFromPlistDictionary: (NSDictionary*) aDict;
@end

@implementation RSSLink (Storage)
-(NSDictionary*) plistDictionary
{
    NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithCapacity: 3];
    
    NSString* desc = [self description];
    NSString* type = [self fileType];
    NSString* rel  = [self relationType];
    
    if (desc != nil) [dict setObject: desc forKey: @"value"];
    if (type != nil) [dict setObject: type forKey: @"type"];
    if (rel  != nil) [dict setObject: rel  forKey: @"rel"];
    
    return dict;
}

+(id) urlFromPlistDictionary: (NSDictionary*) aDict
{
  return [RSSLink linkWithString: [aDict objectForKey: @"value"]
		  andRel: [aDict objectForKey: @"rel"]
		  andType: [aDict objectForKey: @"type"] ];
}
@end




/**
 * Allows to store articles to files and serialize
 * articles into Plist-compatibly dictionaries.
 */
@implementation RSSArticle (Storage)

/**
 * Returns the article with the URL anURL from the storage
 */
+(id<RSSArticle>)articleFromStorageWithURL: (NSString*) anURL
{
    return [[RSSFactory sharedFactory] articleFromDictionary:
                [NSDictionary dictionaryWithContentsOfFile:
                       [[RSSFactory sharedFactory] storagePathForURL: anURL]]];
}

/**
 * Initialises the article with the URL anURL from the storage.
 * 
 * @deprecated
 * 
 * Calling this method is generally a bad idea, since
 * it doesn't allow you to decide on load-time which
 * article is going to be created. Better use one of the
 * RSSFactory methods for article unarchiving.
 */
-(id) initFromStorageWithURL: (NSString*) anURL
{
#ifdef GNUSTEP
    NSDebugLog(@"Calling -initFromStorageWithURL on a concrete RSSArticle class instance");
#endif
    return [self initWithDictionary:
             [NSDictionary dictionaryWithContentsOfFile:
               [[RSSFactory sharedFactory] storagePathForURL: anURL]]];
}

/**
 * Initialises the article instance with the contents of the aDictionary variable.
 */
-(id) initWithDictionary: (NSDictionary*) aDictionary
{
    if ((self = [super init]) != nil) {
	NSArray* arr;
	int i;

        if (aDictionary == nil) {
            DESTROY(self);
            return nil;
        }
        
        ASSIGN( headline,     [aDictionary objectForKey: @"headline"] );
        ASSIGN( url,          [aDictionary objectForKey: @"article URL"] );
        ASSIGN( description,  [aDictionary objectForKey: @"article content"] );
        ASSIGN( date,         [aDictionary objectForKey: @"date"] );
	
	arr = [aDictionary objectForKey: @"links"];
	ASSIGN(links, AUTORELEASE([[NSMutableArray alloc] init]));
	
	for (i=0; i<[arr count]; i++) {
	  [links addObject: [RSSLink urlFromPlistDictionary: [arr objectAtIndex: i]]];
	}
    }
    
    return self;
}

-(NSString*) storagePath
{
    return [[RSSFactory sharedFactory] storagePathForURL: url];
}

/**
 * Stores the article (usually as a file in the Reader folder).
 */
-(BOOL) store
{
    return [[self plistDictionary] writeToFile: [self storagePath] atomically: YES];
}

/**
 * Returns the dictionary that stores the information for this article object.
 */
-(NSMutableDictionary*) plistDictionary
{
    int i;
    NSMutableDictionary* dict;
    NSMutableArray* linksArray;
    
    // Create a (Plist-compatible) array of Dictionaries from
    // the article's link list (an array of NSURL instances)
    linksArray = [NSMutableArray arrayWithCapacity: [links count]];
    for (i=0;i<[links count]; i++) {
        RSSLink* thisURL = [links objectAtIndex: i];
        [linksArray addObject: [thisURL plistDictionary]];
    }
    
    // Create a dictionary from it all.3
    dict = [NSMutableDictionary dictionaryWithCapacity:  10];
    
    if (headline != nil   ) [dict setObject: headline     forKey: @"headline"];
    if (url != nil        ) [dict setObject: url          forKey: @"article URL"];
    if (description != nil) [dict setObject: description  forKey: @"article content"];
    if (date != nil       ) [dict setObject: date         forKey: @"date"];
    
    [dict setObject: linksArray   forKey: @"links"];
    
    return dict;
}

@end