File: AppController.m

package info (click to toggle)
grr.app 1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, stretch, trixie
  • size: 3,948 kB
  • ctags: 118
  • sloc: objc: 4,019; sh: 72; makefile: 18
file content (360 lines) | stat: -rw-r--r-- 13,191 bytes parent folder | download
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
/*
   Grr RSS Reader
   
   Copyright (C) 2006-2007 Guenther Noack <guenther@unix-ag.uni-kl.de>
   Copyright (C) 2009-2012 GNUstep Application Team
                           Riccardo Mottola

   This application is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.
 
   This application 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
   Library General Public License for more details.
 
   You should have received a copy of the GNU General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA. 
*/

#import <AppKit/AppKit.h>

#import "AppController.h"
#import "Components.h"
#import "Database.h"
#import "ToolbarDelegate.h"
#import "ArticleFactory.h"
#import "PreferencesPanel.h"
#import "NSBundle+Extensions.h"

#ifdef __APPLE__
#import "GNUstep.h"
#endif

@interface AppController (Private)
-(void)loadViewProvidingComponent: (id<ViewProvidingComponent>) aComponent overriddenView: (NSView**) overriddenView;
-(BOOL) loadToolbarProvidingComponent: (id<ToolbarDelegate>) aComponent;
-(NSArray*) askToolbarProvidersForArrayUsingSelector: (SEL) aSelector
                                              toolbar: (NSToolbar*) toolbar
                                       optionalMethod: (BOOL) optionalMethod;
@end

@implementation AppController (Private)

/**
 * Loads a view providing component into the place of an existing view object.
 * Please not that the overriddenView parameter is _a_pointer_ to the reference
 * to the overridden view! The new view will also be retained!
 */
-(void)loadViewProvidingComponent: (id<ViewProvidingComponent>) aComponent overriddenView: (NSView**) overriddenView
{
    NSView* newView;
    id<ViewProvidingComponent, NSObject> component = (id<ViewProvidingComponent, NSObject>) aComponent;
    
    NSAssert1(
        [component conformsToProtocol: @protocol(ViewProvidingComponent)],
        @"Grr component %@ provides no view. So it cannot be used as such.", component
    );
    
    NSLog(@"Module %@ is being loaded.", component);
    newView = [component view];
    
    NSAssert1(newView != nil, @"Component %@ had no view, although it should.", component);
    
    [newView setFrame: [*overriddenView frame]];
    [[*overriddenView superview] replaceSubview: *overriddenView with: newView];
    
    ASSIGN(*overriddenView, newView);
}

/**
 * Loads a component that acts as a toolbar delegate.
 * Returns YES on success, NO on failure.
 */
-(BOOL) loadToolbarProvidingComponent: (id<ToolbarDelegate>) aComponent
{
    if ([aComponent conformsToProtocol: @protocol(ToolbarDelegate)] == NO) {
        NSLog(@"Tried to add component not conforming to the ToolbarDelegate protocol: %@", [aComponent class]);
        return NO;
    }
    
    [toolbarProviders addObject: aComponent];
    return YES;
}

/*
 * Helper method that sends a message with the given selector to each
 * registered toolbar provider and concatenates the results of these
 * method calls. The methods belonging to the selector must return
 * a result of the NSArray* type.
 * 
 * If the implementation of the method is optional for the toolbar
 * provider, the optionalMethod flag can be set to YES to let this
 * method check if the method is implemented by the toolbar providers
 * before sending the message to them.
 */
- (NSArray*) askToolbarProvidersForArrayUsingSelector: (SEL) aSelector
                                              toolbar: (NSToolbar*) toolbar
                                       optionalMethod: (BOOL) optionalMethod
{
    NSMutableArray* mutArray = [NSMutableArray new];
    
    int i;
    for (i=0; i<[toolbarProviders count]; i++) {
        id<ToolbarDelegate> provider = [toolbarProviders objectAtIndex: i];
        BOOL executionPossible = NO;
        
        if (optionalMethod == NO) {
            executionPossible = YES;
        } else {
            if ([provider respondsToSelector: aSelector]) {
                executionPossible = YES;
            }
        }
        
        if (executionPossible == YES) {
            [mutArray addObjectsFromArray: [provider performSelector: aSelector
                                                     withObject: toolbar]];
        }
    }
    
    return [NSArray arrayWithArray: mutArray];
}


@end

@implementation AppController

-(void) awakeFromNib
{
    // Retain some views
    [articleSetView retain];
    [articleView retain];
    [databaseView retain];
    
    // toolbar is created in applicationDidFinishLaunching
    
    // Make sure the toolbar provider array is initialized
    ASSIGN(toolbarProviders, [NSMutableArray new]);
}


// ------------------------------------------------
//    Application notifications
// ------------------------------------------------

// Macros for setting font names and sizes in defaults without overwriting
// existing values

// obj is a NSFont object, key is a NSString
#define SET_FAMILY( obj, key )       \
    if ([defaults objectForKey: (key)] == nil) {     \
        [defaults setObject: [(obj) fontName] forKey: (key)];   \
    }

// obj is a float, key is a NSString
#define SET_SIZE( obj, key )       \
    if ([defaults objectForKey: (key)] == nil) {     \
        [defaults setObject: [NSNumber numberWithFloat: (obj)] forKey: (key)];   \
    }


-(void) applicationDidFinishLaunching: (NSNotification*) aNotification
{
    id<OutputProvidingComponent> db;
    id<ViewProvidingComponent,FilterComponent> plugin1;
    id<ViewProvidingComponent,FilterComponent> plugin2;
    id<ViewProvidingComponent,FilterComponent> feedPlugin;
    id<ToolbarDelegate> articleOpsToolbar;
    id<ToolbarDelegate> feedOpsToolbar;
    id<ToolbarDelegate,FilterComponent> searchingComponent;
    NSNotificationCenter* defaultCenter;
    NSUserDefaults* defaults;


    // First create a different RSSFactory and replace the current one with that.
    [RSSFactory setFactory: [[[ArticleFactory alloc] init] autorelease]];
    
    // Make sure the fonts are set.
    defaults = [NSUserDefaults standardUserDefaults];
    SET_FAMILY( [NSFont systemFontOfSize: [NSFont systemFontSize]], @"RSSReaderFeedListFontDefaults" );
    SET_SIZE( [NSFont systemFontSize], @"RSSReaderFeedListSizeDefaults" );
    SET_FAMILY( [NSFont systemFontOfSize: [NSFont systemFontSize]], @"RSSReaderArticleListFontDefaults" );
    SET_SIZE( [NSFont systemFontSize], @"RSSReaderArticleListSizeDefaults" );
    SET_FAMILY( [NSFont userFontOfSize: [NSFont systemFontSize]], @"RSSReaderArticleContentFontDefaults" );
    SET_SIZE( [NSFont systemFontSize], @"RSSReaderArticleContentSizeDefaults" );
    SET_FAMILY( [NSFont userFixedPitchFontOfSize: [NSFont systemFontSize]], @"RSSReaderFixedArticleContentFontDefaults" );
    SET_SIZE( [NSFont systemFontSize], @"RSSReaderFixedArticleContentSizeDefaults" );
    
    
    // Create different Grr components
    db = [Database shared];
    plugin1 = [NSBundle instanceForBundleWithName: @"ArticleTable"];
    plugin2 = [NSBundle instanceForBundleWithName: @"ArticleView"];
    feedPlugin = [NSBundle instanceForBundleWithName: @"DatabaseTreeView"];
    articleOpsToolbar = [NSBundle instanceForBundleWithName: @"ArticleOperations"];
    feedOpsToolbar = [NSBundle instanceForBundleWithName: @"DatabaseOperations"];
    searchingComponent = [NSBundle instanceForBundleWithName: @"Searching"];
    
    NSAssert(plugin1 != nil, @"Article Table plugin could not be loaded.");
    NSAssert(plugin2 != nil, @"Article View plugin could not be loaded.");
    NSAssert(feedPlugin != nil, @"Feed Table plugin could not be loaded.");
    NSAssert(searchingComponent != nil, @"Searching plugin could not be loaded.");
    
    // Connect the created components...
    defaultCenter = [NSNotificationCenter defaultCenter];
    
    [defaultCenter addObserver: searchingComponent
        selector: @selector(componentDidUpdateSet:)
        name: ComponentDidUpdateNotification
        object: feedPlugin];
    
    [defaultCenter addObserver: plugin1
        selector: @selector(componentDidUpdateSet:)
        name: ComponentDidUpdateNotification
        object: searchingComponent];
    
    [defaultCenter addObserver: plugin2
        selector: @selector(componentDidUpdateSet:)
        name: ComponentDidUpdateNotification
        object: plugin1];
    
    [defaultCenter addObserver: articleOpsToolbar
        selector: @selector(componentDidUpdateSet:)
        name: ComponentDidUpdateNotification
        object: plugin1];
    
    [defaultCenter addObserver: feedOpsToolbar
        selector: @selector(componentDidUpdateSet:)
        name: ComponentDidUpdateNotification
        object: feedPlugin];
    
    // Insert the created viewable components into the GUI
    [self loadViewProvidingComponent: plugin1 overriddenView: &articleSetView];
    [self loadViewProvidingComponent: plugin2 overriddenView: &articleView];
    [self loadViewProvidingComponent: feedPlugin overriddenView: &databaseView];
    
    [self loadToolbarProvidingComponent: feedOpsToolbar];
    [self loadToolbarProvidingComponent: articleOpsToolbar];
    [self loadToolbarProvidingComponent: searchingComponent];
    
    [defaultCenter postNotificationName: ComponentDidUpdateNotification object: db];
    
    // Create toolbar
    fToolbar = [(NSToolbar*)[NSToolbar alloc] initWithIdentifier: @"main window toolbar"];
    [fToolbar setDelegate: self];
    
    [window setToolbar: fToolbar];

    // Set the autosave name for the window to let the window
    // appear in the same place next time, too. :)
    [window setFrameAutosaveName: @"Grr main window"];
    
    // Now that everything is loaded, we can show the window.
    [window makeKeyAndOrderFront: self];
    
    // Application startup is finished. See applicationDidBecomeActive: below.
    applicationStartupFinished = YES;
}

- (void) applicationDidBecomeActive: (NSNotification*) aNotification
{
    // Only order front if application startup is finished. If we didn't do this,
    // the window would instantly be shown, as this method is called before it
    // is supposed to be shown. (That is, when all the plugin bundles are loaded.)
    if (applicationStartupFinished) {
        [window makeKeyAndOrderFront: self];
    }
}

- (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication*) application
{
    if ([[Database shared] archive] == NO) {
        NSLog(@"Database could not be saved to disk"); // FIXME: better error handling needed!
    }
    
    // toolbar must be removed before terminating, so that the window
    // can still store its *real* location, not the location the GNUstep
    // impl of NSToolbar messed up.
    [fToolbar setVisible: NO];
    return NSTerminateNow;
}

// ------------------------------------------------
//    Opening files
// ------------------------------------------------

- (BOOL) application: (NSApplication *) application
            openFile: (NSString *) fileName
{
    NSURL* URL = [NSURL fileURLWithPath: fileName];
    
    if (URL == nil) {
        return NO;
    }
    
    return [[Database shared] subscribeToURL: URL];
}


// ------------------------------------------------
//    Handlers for actions
// ------------------------------------------------

-(IBAction) openPreferencesPanel: (id)sender
{
    [[PreferencesPanel shared] open];
}

// ------------------------------------------------
//    Toolbar delegate
// ------------------------------------------------

// required method
- (NSToolbarItem*)toolbar: (NSToolbar*)toolbar
    itemForItemIdentifier: (NSString*)itemIdentifier
willBeInsertedIntoToolbar: (BOOL)flag
{
    NSToolbarItem* result = nil;
    
    int i;
    for (i=0; i<[toolbarProviders count] && result == nil; i++) {
        id<ToolbarDelegate> provider = [toolbarProviders objectAtIndex: i];
        
        result = [provider toolbar: toolbar
                    itemForItemIdentifier: itemIdentifier
                    willBeInsertedIntoToolbar: flag];
    }
    
    return result;
}

// required method
- (NSArray*) toolbarAllowedItemIdentifiers: (NSToolbar*)toolbar
{
    return [self askToolbarProvidersForArrayUsingSelector: _cmd
                                                  toolbar: toolbar
                                           optionalMethod: YES];
}

// required method
- (NSArray*) toolbarDefaultItemIdentifiers: (NSToolbar*)toolbar;
{
    return [self askToolbarProvidersForArrayUsingSelector: _cmd
                                                  toolbar: toolbar
                                           optionalMethod: YES];
}

// optional method
- (NSArray*) toolbarSelectableItemIdentifiers: (NSToolbar *)toolbar;
{
    return [self askToolbarProvidersForArrayUsingSelector: _cmd
                                                  toolbar: toolbar
                                           optionalMethod: YES];
}

@end