File: FModuleCrDate.m

package info (click to toggle)
gworkspace 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,124 kB
  • sloc: objc: 71,982; sh: 488; makefile: 43
file content (485 lines) | stat: -rw-r--r-- 13,199 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
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
/* FModuleCrDate.m
 *  
 * Copyright (C) 2004-2016 Free Software Foundation, Inc.
 *
 * Author: Enrico Sersale <enrico@imago.ro>
 * Date: March 2004
 *
 * This file is part of the GNUstep GWorkspace application
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
 */

#include <math.h>

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import "FinderModulesProtocol.h"

static NSString *nibName = @"FModuleCrDate";

@interface FModuleCrDate : NSObject <FinderModulesProtocol>
{  
  IBOutlet id win;
  IBOutlet NSBox *controlsBox;
  IBOutlet id isPopUp;
  IBOutlet id whenPopUp;
  IBOutlet id dateField;
  IBOutlet id dateStepper;
  double stepperValue;
  NSInteger index;
  BOOL used;

  NSFileManager *fm;
  NSCalendarDate *date;
  NSTimeInterval interval;
  NSInteger how;
}

- (IBAction)popUpAction:(id)sender; 

- (IBAction)stepperAction:(id)sender; 

@end

@implementation FModuleCrDate

#define TODAY   0
#define WITHIN  1
#define BEFORE  2
#define AFTER   3
#define EXACTLY 4

#define LAST_DAY     0
#define LAST_2DAYS   1
#define LAST_3DAYS   2
#define LAST_WEEK    3
#define LAST_2WEEKS  4
#define LAST_3WEEKS  5
#define LAST_MONTH   6
#define LAST_2MONTHS 7
#define LAST_3MONTHS 8
#define LAST_6MONTHS 9

#define MINUTE_TI (60.0)
#define HOUR_TI   (MINUTE_TI * 60)
#define DAY_TI    (HOUR_TI * 24)
#define DAYS2_TI  (DAY_TI * 2)
#define DAYS3_TI  (DAY_TI * 3)
#define WEEK_TI   (DAY_TI * 7)
#define WEEK2_TI  (WEEK_TI * 2)
#define WEEK3_TI  (WEEK_TI * 3)
#define MONTH_TI  (DAY_TI * 30)
#define MONTH2_TI ((MONTH_TI * 2) + DAY_TI)
#define MONTH3_TI ((MONTH_TI * 3) + (DAY_TI * 1.5))
#define MONTH6_TI ((MONTH_TI * 6) + (DAY_TI * 3))

- (void)dealloc
{
  RELEASE (controlsBox);
  RELEASE (whenPopUp);
  RELEASE (dateField);
  RELEASE (dateStepper); 
  RELEASE (date);
  [super dealloc];
}

- (id)initInterface
{
	self = [super init];

  if (self) {
    NSDateFormatter *formatter;
    NSRect r;
    
		if ([NSBundle loadNibNamed: nibName owner: self] == NO) {
      NSLog(@"failed to load %@!", nibName);
      DESTROY (self);
      return self;
    }

    RETAIN (controlsBox);
    RELEASE (win);

    used = NO;
    index = 0;
    
    [dateField setStringValue: @""];
    r = [dateField frame];
    r.origin.y = 0;
    [dateField setFrame: r];
    
    r = [dateStepper frame];
    r.origin.y = 0;
    [dateStepper setFrame: r];
    [dateStepper setMaxValue: MONTH6_TI];
    [dateStepper setMinValue: 0];
    [dateStepper setIncrement: 1];
    [dateStepper setAutorepeat: YES];
    [dateStepper setValueWraps: YES];
    
    stepperValue = MONTH3_TI;
    [dateStepper setDoubleValue: stepperValue];
    
    RETAIN (whenPopUp);
    RETAIN (dateField);
    RETAIN (dateStepper);
    
    [whenPopUp removeFromSuperview];
    
    formatter = [[NSDateFormatter alloc] initWithDateFormat: @"%m %d %Y"
                                       allowNaturalLanguage: NO];
    [[dateField cell] setFormatter: formatter];
    RELEASE (formatter);
    
    /* Internationalization */    
    [isPopUp removeAllItems];
    [isPopUp insertItemWithTitle: NSLocalizedString(@"is today", @"") atIndex: TODAY];
    [isPopUp insertItemWithTitle: NSLocalizedString(@"is within", @"") atIndex: WITHIN];
    [isPopUp insertItemWithTitle: NSLocalizedString(@"is before", @"") atIndex: BEFORE];
    [isPopUp insertItemWithTitle: NSLocalizedString(@"is after", @"") atIndex: AFTER];
    [isPopUp insertItemWithTitle: NSLocalizedString(@"is exactly", @"") atIndex: EXACTLY];
    [isPopUp selectItemAtIndex: TODAY]; 

    [whenPopUp removeAllItems];
    [whenPopUp insertItemWithTitle: NSLocalizedString(@"the last day", @"") atIndex: LAST_DAY];
    [whenPopUp insertItemWithTitle: NSLocalizedString(@"the last 2 days", @"") atIndex: LAST_2DAYS];
    [whenPopUp insertItemWithTitle: NSLocalizedString(@"the last 3 days", @"") atIndex: LAST_3DAYS];
    [whenPopUp insertItemWithTitle: NSLocalizedString(@"the last week", @"") atIndex: LAST_WEEK];
    [whenPopUp insertItemWithTitle: NSLocalizedString(@"the last 2 weeks", @"") atIndex: LAST_2WEEKS];
    [whenPopUp insertItemWithTitle: NSLocalizedString(@"the last 3 weeks", @"") atIndex: LAST_3WEEKS];
    [whenPopUp insertItemWithTitle: NSLocalizedString(@"the last month", @"") atIndex: LAST_MONTH];
    [whenPopUp insertItemWithTitle: NSLocalizedString(@"the last 2 months", @"") atIndex: LAST_2MONTHS];
    [whenPopUp insertItemWithTitle: NSLocalizedString(@"the last 3 months", @"") atIndex: LAST_3MONTHS];
    [whenPopUp insertItemWithTitle: NSLocalizedString(@"the last 6 months", @"") atIndex: LAST_6MONTHS];
    [whenPopUp selectItemAtIndex: LAST_DAY]; 
  }
  
  return self;
}

- (id)initWithSearchCriteria:(NSDictionary *)criteria
                  searchTool:(id)tool
{
	self = [super init];

  if (self) {
    how = [[criteria objectForKey: @"how"] intValue];

    if ((how == TODAY) || (how == WITHIN)) {
      interval = [[criteria objectForKey: @"limit"] doubleValue];
    } else {
      ASSIGN (date, [criteria objectForKey: @"date"]);
    }
    
    fm = [NSFileManager defaultManager];
  }
  
	return self;
}

- (IBAction)popUpAction:(id)sender
{
  if (sender == isPopUp) {
    NSInteger idx = [sender indexOfSelectedItem];
    NSView *view = [controlsBox contentView];
    NSArray *views = [view subviews];

    if (idx == TODAY) {
      if ([views containsObject: dateField]) {
        [dateField removeFromSuperview];
        [dateStepper removeFromSuperview];
      }
      if ([views containsObject: whenPopUp]) {
        [whenPopUp removeFromSuperview];
      }
      
    } else if (idx == WITHIN) {
      if ([views containsObject: dateField]) {
        [dateField removeFromSuperview];
        [dateStepper removeFromSuperview];
      }
      if ([views containsObject: whenPopUp] == NO) {
        [view addSubview: whenPopUp];
      }
      
    } else if ((idx == BEFORE) || (idx == AFTER) || (idx == EXACTLY)) {
      if ([views containsObject: whenPopUp]) {
        [whenPopUp removeFromSuperview];
      }

      if ([views containsObject: dateField] == NO) {
        NSCalendarDate *cdate = [NSCalendarDate calendarDate];
        int month = [cdate monthOfYear];
        int day = [cdate dayOfMonth];
        int year = [cdate yearOfCommonEra];
        NSString *str = [NSString stringWithFormat: @"%i %i %i", month, day, year];
      
        [view addSubview: dateField];
        [dateField setStringValue: str];
        [view addSubview: dateStepper];
      }
    }
  }
}

- (IBAction)stepperAction:(id)sender
{
  NSString *str = [dateField stringValue];  

  if ([str length]) {
    NSCalendarDate *cdate = [NSCalendarDate dateWithString: str
                                            calendarFormat: @"%m %d %Y"];
    if (cdate) {   
      double sv = [sender doubleValue];
      int month, day, year;
    
      if (sv > stepperValue) {
        cdate = [cdate addTimeInterval: DAY_TI];
      } else if (sv < stepperValue) {
        cdate = [cdate addTimeInterval: -DAY_TI];
      }
    
      month = [cdate monthOfYear];
      day = [cdate dayOfMonth];
      year = [cdate yearOfCommonEra];    

      str = [NSString stringWithFormat: @"%i %i %i", month, day, year];
      [dateField setStringValue: str];

      stepperValue = sv;
    }
  } 
}

- (void)setControlsState:(NSDictionary *)info
{
  NSNumber *num = [info objectForKey: @"how"];
  
  if (num) {
    NSInteger idx = [num integerValue];

    [isPopUp selectItemAtIndex: idx];
    [self popUpAction: isPopUp];

    if (idx == WITHIN) {
      NSNumber *limnum = [info objectForKey: @"limit"];
      int whenidx = 0;

      if (limnum) {
        double limit = [limnum doubleValue];
      
        if (limit == DAY_TI) {
          whenidx = LAST_DAY;
        } else if (limit == DAYS2_TI) {
          whenidx = LAST_2DAYS;
        } else if (limit == DAYS3_TI) {
          whenidx = LAST_3DAYS;
        } else if (limit == WEEK_TI) {
          whenidx = LAST_WEEK;
        } else if (limit == WEEK2_TI) {
          whenidx = LAST_2WEEKS;
        } else if (limit == WEEK3_TI) {
          whenidx = LAST_3WEEKS;
        } else if (limit == MONTH_TI) {
          whenidx = LAST_MONTH;
        } else if (limit == MONTH2_TI) {
          whenidx = LAST_2MONTHS;
        } else if (limit == MONTH3_TI) {
          whenidx = LAST_3MONTHS;
        } else if (limit == MONTH6_TI) {
          whenidx = LAST_6MONTHS;
        } 
      }

      [whenPopUp selectItemAtIndex: whenidx];
    
    } else if ((idx == BEFORE) || (idx == AFTER) || (idx == EXACTLY)) {
      NSCalendarDate *cdate = [info objectForKey: @"date"];

      if (cdate) {
        int month = [cdate monthOfYear];
        int day = [cdate dayOfMonth];
        int year = [cdate yearOfCommonEra];
        NSString *str = [NSString stringWithFormat: @"%i %i %i", month, day, year];
      
        [dateField setStringValue: str];
      }
    }    
  }  
}

- (id)controls
{
  return controlsBox;
}

- (NSString *)moduleName
{
  return NSLocalizedString(@"date created", @"");
}

- (BOOL)used
{
  return used;
}

- (void)setInUse:(BOOL)value
{
  used = value;
}

- (NSInteger)index
{
  return index;
}

- (void)setIndex:(NSInteger)idx
{
  index = idx;
}

- (NSDictionary *)searchCriteria
{
  NSMutableDictionary *criteria = [NSMutableDictionary dictionary];
  NSCalendarDate *cdate = [NSCalendarDate calendarDate];
  NSTimeInterval limit = 0.0;
  NSInteger idx = [isPopUp indexOfSelectedItem];
  
  if (idx == TODAY) {
    NSCalendarDate *midnight;

    midnight = [NSCalendarDate dateWithYear: [cdate yearOfCommonEra]
                                      month: [cdate monthOfYear]
                                        day: [cdate dayOfMonth]
                                       hour: 0
                                     minute: 0
                                     second: 0
                                   timeZone: [cdate timeZone]];
    
    limit = [midnight timeIntervalSinceNow];
         
  } else if (idx == WITHIN) {
    int when = [whenPopUp indexOfSelectedItem];

    switch(when) {
      case LAST_DAY:
        limit = DAY_TI;
        break;
      case LAST_2DAYS:
        limit = DAYS2_TI;
        break;
      case LAST_3DAYS:
        limit = DAYS3_TI;
        break;
      case LAST_WEEK:
        limit = WEEK_TI;
        break;
      case LAST_2WEEKS:
        limit = WEEK2_TI;
        break;
      case LAST_3WEEKS:
        limit = WEEK3_TI;
        break;
      case LAST_MONTH:
        limit = MONTH_TI;
        break;
      case LAST_2MONTHS:
        limit = MONTH2_TI;
        break;
      case LAST_3MONTHS:
        limit = MONTH3_TI;
        break;
      case LAST_6MONTHS:
        limit = MONTH6_TI;
        break;
    }
      
  } else if ((idx == BEFORE) || (idx == AFTER) || (idx == EXACTLY)) {
    NSString *str = [dateField stringValue];  

    if ([str length]) { 
      cdate = [NSCalendarDate dateWithString: str
                              calendarFormat: @"%m %d %Y"];
    }  
  }

  [criteria setObject: [NSNumber numberWithDouble: limit] forKey: @"limit"];
  [criteria setObject: cdate forKey: @"date"];
  [criteria setObject: [NSNumber numberWithInt: idx] forKey: @"how"];

  return criteria;
}

- (BOOL)checkPath:(NSString *)path 
   withAttributes:(NSDictionary *)attributes
{
  NSDate *cd = [attributes fileCreationDate];

  if (how == TODAY) {
    return (interval <= [cd timeIntervalSinceNow]);

  } else if (how == WITHIN) {
    return (fabs([cd timeIntervalSinceNow]) <= interval);

  } else if ((how == BEFORE) || (how == AFTER) || (how == EXACTLY)) {      
    NSCalendarDate *cdate = [cd dateWithCalendarFormat: [date calendarFormat] 
                                              timeZone: [date timeZone]];

    if (how == BEFORE) { 
      if ([[date earlierDate: cd] isEqualToDate: cd]) {
        return ([cdate dayOfMonth] != [date dayOfMonth]);
      }

    } else if (how == AFTER) { 
      if ([[date earlierDate: cd] isEqualToDate: date]) {
        return ([cdate dayOfMonth] != [date dayOfMonth]);
      }

    } else if (how == EXACTLY) {
      if (fabs([cd timeIntervalSinceDate: date]) < DAY_TI) {
        return ([cdate dayOfMonth] == [date dayOfMonth]);
      }  
    }
  }    
  
  return NO;
}

- (NSComparisonResult)compareModule:(id <FinderModulesProtocol>)module
{
  NSInteger i1 = [self index];
  NSInteger i2 = [module index];

  if (i1 < i2) {
    return NSOrderedAscending;
  } else if (i1 > i2) {
    return NSOrderedDescending;
  } 

  return NSOrderedSame;
}

- (BOOL)reliesOnModDate
{
  return NO;
}

- (BOOL)metadataModule
{
  return NO;
}

@end