1/* CategoriesEditor.m
2 *
3 * Copyright (C) 2006 Free Software Foundation, Inc.
4 *
5 * Author: Enrico Sersale <enrico@imago.ro>
6 * Date: December 2006
7 *
8 * This file is part of the GNUstep GWorkspace application
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
23 */
24
25#include <AppKit/AppKit.h>
26#include "CategoriesEditor.h"
27#include "CategoryView.h"
28#include "MDIndexing.h"
29
30#define LINEH (28.0)
31
32@implementation CategoriesEditor
33
34- (void)dealloc
35{
36  RELEASE (categories);
37  RELEASE (catviews);
38
39  [super dealloc];
40}
41
42- (id)initWithFrame:(NSRect)rect
43{
44  self = [super initWithFrame: rect];
45
46  if (self) {
47    NSBundle *bundle = [NSBundle bundleForClass: [self class]];
48    NSString *dictpath = [bundle pathForResource: @"categories" ofType: @"plist"];
49    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: dictpath];
50    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
51    NSDictionary *domain;
52    NSArray *catnames;
53    unsigned i;
54
55    if (dict == nil) {
56      [NSException raise: NSInternalInconsistencyException
57		              format: @"\"%@\" doesn't contain a dictionary!", dictpath];
58    }
59
60    [defaults synchronize];
61    domain = [defaults persistentDomainForName: @"MDKQuery"];
62
63    if (domain == nil) {
64      domain = [NSDictionary dictionaryWithObject: dict forKey: @"categories"];
65      [defaults setPersistentDomain: domain forName: @"MDKQuery"];
66      [defaults synchronize];
67
68    } else {
69      NSDictionary *catdict = [domain objectForKey: @"categories"];
70
71      if ((catdict == nil) || ([catdict count] == 0)) {
72        NSMutableDictionary *mdom = [domain mutableCopy];
73
74        [mdom setObject: dict forKey: @"categories"];
75        [defaults setPersistentDomain: mdom forName: @"MDKQuery"];
76        [defaults synchronize];
77        RELEASE (mdom);
78      }
79    }
80
81    categories = [[domain objectForKey: @"categories"] mutableCopy];
82    catnames = [categories keysSortedByValueUsingSelector: @selector(compareAccordingToIndex:)];
83    catviews = [NSMutableArray new];
84
85    for (i = 0; i < [catnames count]; i++) {
86      NSString *catname = [catnames objectAtIndex: i];
87      NSDictionary *catinfo = [categories objectForKey: catname];
88      CategoryView *cview = [[CategoryView alloc] initWithCategoryInfo: catinfo
89                                                              inEditor: self];
90      [catviews addObject: cview];
91      [self addSubview: cview];
92      RELEASE (cview);
93    }
94  }
95
96  return self;
97}
98
99- (void)setMdindexing:(id)anobject
100{
101  mdindexing = anobject;
102  [mdindexing searchResultDidEndEditing];
103}
104
105- (void)categoryViewDidChangeState:(CategoryView *)view
106{
107  [mdindexing searchResultDidStartEditing];
108}
109
110- (void)moveCategoryViewAtIndex:(int)srcind
111                        toIndex:(int)dstind
112{
113  CategoryView *view = [catviews objectAtIndex: srcind];
114  id dummy = [NSString string];
115  int i;
116
117  RETAIN (view);
118  [catviews replaceObjectAtIndex: srcind withObject: dummy];
119  [catviews insertObject: view atIndex: dstind];
120  [catviews removeObject: dummy];
121  RELEASE (view);
122
123  for (i = 0; i < [catviews count]; i++) {
124    [[catviews objectAtIndex: i] setIndex: i];
125  }
126
127  [self tile];
128  [self setNeedsDisplay: YES];
129  [mdindexing searchResultDidStartEditing];
130}
131
132- (void)applyChanges
133{
134  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
135  NSMutableDictionary *newcat = [NSMutableDictionary dictionary];
136  NSMutableDictionary *domain;
137  int i;
138
139  for (i = 0; i < [catviews count]; i++) {
140    NSDictionary *catinfo = [[catviews objectAtIndex: i] categoryInfo];
141    NSString *catname = [catinfo objectForKey: @"category_name"];
142
143    [newcat setObject: catinfo forKey: catname];
144  }
145
146  [defaults synchronize];
147  domain = [[defaults persistentDomainForName: @"MDKQuery"] mutableCopy];
148  [domain setObject: newcat forKey: @"categories"];
149  [defaults setPersistentDomain: domain forName: @"MDKQuery"];
150  RELEASE (domain);
151  [defaults synchronize];
152
153  [[NSDistributedNotificationCenter defaultCenter]
154           postNotificationName: @"MDKQueryCategoriesDidChange"
155	 								       object: nil
156                       userInfo: nil];
157
158  [mdindexing searchResultDidEndEditing];
159}
160
161- (void)revertChanges
162{
163  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
164  NSDictionary *domain;
165  NSArray *catnames;
166  unsigned i;
167
168  [defaults synchronize];
169  domain = [defaults persistentDomainForName: @"MDKQuery"];
170
171  DESTROY (categories);
172  categories = [[domain objectForKey: @"categories"] mutableCopy];
173  catnames = [categories keysSortedByValueUsingSelector: @selector(compareAccordingToIndex:)];
174
175  while ([catviews count]) {
176    CategoryView *cview = [catviews objectAtIndex: 0];
177
178    [cview removeFromSuperview];
179    [catviews removeObject: cview];
180  }
181
182  for (i = 0; i < [catnames count]; i++) {
183    NSString *catname = [catnames objectAtIndex: i];
184    NSDictionary *catinfo = [categories objectForKey: catname];
185    CategoryView *cview = [[CategoryView alloc] initWithCategoryInfo: catinfo
186                                                            inEditor: self];
187    [catviews addObject: cview];
188    [self addSubview: cview];
189    RELEASE (cview);
190  }
191
192  [self tile];
193
194  [mdindexing searchResultDidEndEditing];
195}
196
197- (void)tile
198{
199  NSView *sview = [self superview];
200  float sh = (sview != nil) ? [sview bounds].size.height : 0.0;
201  NSRect rect = [self frame];
202  int count = [catviews count];
203  float vspace = count * LINEH;
204  int i;
205
206  rect.size.height = (vspace > sh) ? vspace : sh;
207  vspace = rect.size.height;
208  [self setFrame: rect];
209
210  for (i = 0; i < count; i++) {
211    vspace -= LINEH;
212    [[catviews objectAtIndex: i] setFrameOrigin: NSMakePoint(0, vspace)];
213  }
214}
215
216- (void)viewDidMoveToSuperview
217{
218  [super viewDidMoveToSuperview];
219  [self tile];
220}
221
222@end
223
224
225@implementation NSDictionary (CategorySort)
226
227- (NSComparisonResult)compareAccordingToIndex:(NSDictionary *)dict
228{
229  NSNumber *p1 = [self objectForKey: @"index"];
230  NSNumber *p2 = [dict objectForKey: @"index"];
231  return [p1 compare: p2];
232}
233
234@end
235
236