File: Tools.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 (475 lines) | stat: -rw-r--r-- 12,855 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
/* Tools.m
 *  
 * Copyright (C) 2004-2024 Free Software Foundation, Inc.
 *
 * Authors: Enrico Sersale
 *          Riccardo Mottola
 * Date: January 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.
 */

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#include <math.h>
#import "Tools.h"
#import "Inspector.h"
#import "IconView.h"
#import "Functions.h"
#import "FSNodeRep.h"

#define ICNSIZE 48

static NSString *nibName = @"Tools";

@implementation Tools

- (void)dealloc
{
  RELEASE (toolsBox); 
  RELEASE (errLabel); 
  RELEASE (mainBox);   
  RELEASE (insppaths);
  RELEASE (extensions);
  RELEASE (currentApp);
  
  [super dealloc];
}

- (instancetype)initForInspector:(id)insp
{
  self = [super init];
  
  if (self)
    {
      NSRect r;
      id cell;
  
      if ([NSBundle loadNibNamed: nibName owner: self] == NO)
        {
          NSLog(@"failed to load %@!", nibName);
          DESTROY (self);
          return self;
        } 
    
      RETAIN (mainBox);
      RETAIN (toolsBox);
      RELEASE (win); 

      inspector = insp;
      [iconView setInspector: inspector];
      ws = [NSWorkspace sharedWorkspace];

      [scrollView setBorderType: NSBezelBorder];
      [scrollView setHasHorizontalScroller: YES];
      [scrollView setHasVerticalScroller: NO]; 

      cell = [NSButtonCell new];
      [cell setButtonType: NSPushOnPushOffButton];
      [cell setImagePosition: NSImageOnly]; 

      matrix = [[NSMatrix alloc] initWithFrame: NSZeroRect
                                          mode: NSRadioModeMatrix prototype: cell
                                  numberOfRows: 0 numberOfColumns: 0];
      RELEASE (cell);
      [matrix setIntercellSpacing: NSZeroSize];
      [matrix setCellSize: NSMakeSize(64, [[scrollView contentView] bounds].size.height)];
      [matrix setAllowsEmptySelection: YES];
      [matrix setTarget: self];		
      [matrix setAction: @selector(setCurrentApplication:)];		
      [matrix setDoubleAction: @selector(openFile:)];		    
      [scrollView setDocumentView: matrix];	
      RELEASE (matrix);

      r = [toolsBox bounds];
      r.origin.y = 165;
      r.size.height = 25;
      errLabel = [[NSTextField alloc] initWithFrame: r];	
      [errLabel setAlignment: NSCenterTextAlignment];
      [errLabel setFont: [NSFont systemFontOfSize: 18]];
      [errLabel setBackgroundColor: [NSColor windowBackgroundColor]];
      [errLabel setTextColor: [NSColor darkGrayColor]];	
      [errLabel setBezeled: NO];
      [errLabel setEditable: NO];
      [errLabel setSelectable: NO];
      [errLabel setStringValue: NSLocalizedString(@"No Tools Inspector", @"")];

      insppaths = nil;
      currentApp = nil;
      extensions = nil;

      [okButt setTitle: NSLocalizedString(@"Set Default", @"")];
      [okButt setEnabled: NO]; 
    }
  
  return self;
}

- (NSView *)inspView
{
  return mainBox;
}

- (NSString *)winname
{
  return NSLocalizedString(@"Tools Inspector", @"");
}

- (void)activateForPaths:(NSArray *)paths
{
  BOOL toolsOK = YES;
  NSInteger i;

  if (paths == nil)
    {
      DESTROY (insppaths);
      return;
    }

  [okButt setEnabled: NO];		  

  if ([paths count] == 1)
    { 
      FSNode *node = [FSNode nodeWithPath: [paths objectAtIndex: 0]];
      NSImage *icon = [[FSNodeRep sharedInstance] iconOfSize: ICNSIZE forNode: node];
      
      [iconView setImage: icon];
      [titleField setStringValue: [node name]];
    }
  else
    {
      NSImage *icon = [[FSNodeRep sharedInstance] multipleSelectionIconOfSize: ICNSIZE];
      NSString *items;

      items = [NSString stringWithFormat: @"%lu %@",
                        (long unsigned)[paths count],
                        NSLocalizedString(@"Items", @"")];
      [titleField setStringValue: items];  
      [iconView setImage: icon];
    }
   
  for (i = 0; i < [paths count]; i++)
    {
      FSNode *node = [FSNode nodeWithPath: [paths objectAtIndex: i]];
  
      if (![node isValid])
        {  
          toolsOK = NO;
          break;
        }
  }

  // We have a valid node and found applications to open it
  if (toolsOK && [self findApplicationsForPaths: paths])
    {
      [errLabel removeFromSuperview];
      [mainBox addSubview: toolsBox];
    }
  else
    {
      [toolsBox removeFromSuperview];
      [mainBox addSubview: errLabel];
    }
}

- (BOOL)findApplicationsForPaths:(NSArray *)paths
{
  NSMutableDictionary *extensionsAndApps;
  NSMutableArray *commonApps;   
  NSString *s;
  NSCell *cell;
  BOOL appsforext;
  NSInteger i, count;

  ASSIGN (insppaths, paths);

  RELEASE (extensions);
  extensions = [NSMutableArray new];
  extensionsAndApps = [NSMutableDictionary dictionary];

  DESTROY (currentApp);
  [defAppField setStringValue: @""];
  [defPathField setStringValue: @""];
  
  appsforext = YES;
	
  for (i = 0; i < [insppaths count]; i++)
    {
      NSString *ext = [[insppaths objectAtIndex: i] pathExtension];		
      
      if ([extensions containsObject: ext] == NO)
        { 
          NSDictionary *extinfo = [ws infoForExtension: ext];
          
          if (extinfo)
            {
              NSMutableArray *appsnames = [NSMutableArray arrayWithCapacity: 1];
              [appsnames addObjectsFromArray: [extinfo allKeys]];
              [extensionsAndApps setObject: appsnames forKey: ext];
              [extensions addObject: ext];				
            }
          else
            {
              appsforext = NO;
            }
        }            
    }

  if (!appsforext)
    return NO;

  if ([extensions count] == 1) {
    NSString *ext = [extensions objectAtIndex: 0];
    commonApps = [NSMutableArray arrayWithArray: [extensionsAndApps objectForKey: ext]];    
    currentApp = [ws getBestAppInRole: nil forExtension: ext];
    RETAIN (currentApp);			
		
  }
  else
    {
      NSInteger j, n;
		
      for (i = 0; i < [extensions count]; i++)
        {
          NSString *ext1 = [extensions objectAtIndex: i];
          NSMutableArray *a1 = [extensionsAndApps objectForKey: ext1];			
			
          for (j = 0; j < [extensions count]; j++)
            {
              NSString *ext2 = [extensions objectAtIndex: j];
              NSMutableArray *a2 = [extensionsAndApps objectForKey: ext2];
				
              count = [a1 count];			
              for (n = 0; n < count; n++) {
                NSString *s = [a1 objectAtIndex: n];
                if ([a2 containsObject: s] == NO)
                  {
                    [a1 removeObject: s];
                    count--;
                    n--;
                  }
              }
              [extensionsAndApps setObject: a1 forKey: ext1];
            }
        }

      commonApps = [NSMutableArray array];

      for (i = 0; i < [extensions count]; i++)
        {
          NSString *ext = [extensions objectAtIndex: i];
          NSArray *apps = [extensionsAndApps objectForKey: ext];
			
          for (j = 0; j < [apps count]; j++)
            {
              NSString *app = [apps objectAtIndex: j];
              if ([commonApps containsObject: app] == NO)
                {
                  [commonApps addObject: app];
                }
            }
        }
		
      if ([commonApps count] != 0)
        {
          BOOL iscommapp = YES;		
          NSString *ext1 = [extensions objectAtIndex: 0];

          currentApp = [ws getBestAppInRole: nil forExtension: ext1];
			
          if ([commonApps containsObject: currentApp])
            {
              for (i = 1; i < [extensions count]; i++)
                {
                  NSString *ext2 = [extensions objectAtIndex: i];
                  NSString *app = [ws getBestAppInRole: nil forExtension: ext2];

                  if ([currentApp isEqual: app] == NO)
                    {
                      iscommapp = NO;
                    }
                }
            }
          else
            {
              currentApp = nil;
            }

          if ((iscommapp == YES) && (currentApp != nil) && appsforext)
            {
              RETAIN (currentApp);		
            }
          else
            {
              currentApp = nil;
            }
        }
    }

  if (currentApp == nil)
    return NO;

  if ([commonApps count] == 0)
    return NO;

  [okButt setEnabled: YES];

  count = [commonApps count];

  [matrix renewRows: 1 columns: count];
  [matrix sizeToCells];

  if (appsforext)
    {
      for (i = 0; i < count; i++)
	{
	  NSString *appName = [commonApps objectAtIndex: i];
	  FSNode *node = [FSNode nodeWithPath: [ws fullPathForApplication: appName]];
	  NSImage *icon = [[FSNodeRep sharedInstance] iconOfSize: ICNSIZE forNode: node];

	  cell = [matrix cellAtRow: 0 column: i];
	  [cell setImage: icon];
	  [cell setTitle: appName];
	}

      [matrix sizeToCells];
    }
	
  if (currentApp != nil)
    {
      NSArray *cells = [matrix cells];
		
      for(i = 0; i < [cells count]; i++)
	{
	  cell = [cells objectAtIndex: i];
	  if(cell && ([[cell title] isEqualToString: currentApp]))
	    {
	      [matrix selectCellAtRow: 0 column: i];
	      [matrix scrollCellToVisibleAtRow: 0 column: i];
	      break;
	    }
	}

      [defAppField setStringValue: [currentApp stringByDeletingPathExtension]];
      s = [ws fullPathForApplication: currentApp];
      if (s != nil)
	s = relativePathFit(defPathField, s);
      else
	s = @"";
      [defPathField setStringValue: s];
    }

  return YES;
}

- (void)setCurrentApplication:(id)sender
{
  NSString *s;
	
  ASSIGN (currentApp, [[sender selectedCell] title]);	
  s = [ws fullPathForApplication: currentApp];
  s = relativePathFit(defPathField, s);
  [defPathField setStringValue: s];
  [defAppField setStringValue: [currentApp stringByDeletingPathExtension]];  
}

- (IBAction)setDefaultApplication:(id)sender
{
  NSString *ext, *app;
  NSDictionary *changedInfo;
  NSArray *cells;
  NSMutableArray *newApps;
  id cell;
  FSNode *node;
  NSImage *icon;
  NSInteger i, count;
  
  for (i = 0; i < [extensions count]; i++)
    {
      ext = [extensions objectAtIndex: i];  		
      [ws setBestApp: currentApp inRole: nil forExtension: ext];  
    }

  changedInfo = [NSDictionary dictionaryWithObjectsAndKeys: 
                                currentApp, @"app", 
                              extensions, @"exts", nil];
  
  [[NSDistributedNotificationCenter defaultCenter]
 				   postNotificationName: @"GWAppForExtensionDidChangeNotification"
                                                 object: nil
                                               userInfo: changedInfo];  
  
  newApps = [NSMutableArray arrayWithCapacity: 1];
  [newApps addObject: currentApp];
  
  cells = [matrix cells];
  for(i = 0; i < [cells count]; i++)
    {
      app = [[cells objectAtIndex: i] title];
      if ([app isEqual: currentApp] == NO)
        {
          [newApps insertObject: app atIndex: [newApps count]];
        }
    }
  
  count = [newApps count];
  [matrix renewRows: 1 columns: count];
  
  for (i = 0; i < count; i++)
    {
      cell = [matrix cellAtRow: 0 column: i];
      app = [newApps objectAtIndex: i];
      [cell setTitle: app];
      node = [FSNode nodeWithPath: [ws fullPathForApplication: app]];
      icon = [[FSNodeRep sharedInstance] iconOfSize: ICNSIZE forNode: node];
      [cell setImage: icon];
    }

  [matrix scrollCellToVisibleAtRow: 0 column: 0];
  [matrix selectCellAtRow: 0 column: 0];
}

- (void)openFile:(id)sender
{
  NSInteger i;
  
  for (i = 0; i < [insppaths count]; i++)
    {
      NSString *fpath = [insppaths objectAtIndex: i];
        
      NS_DURING
        {
          [ws openFile: fpath withApplication: [[sender selectedCell] title]];
        }
      NS_HANDLER
        {
          NSRunAlertPanel(NSLocalizedString(@"error", @""), 
                          [NSString stringWithFormat: @"%@ %@!", 
                                    NSLocalizedString(@"Can't open ", @""), [fpath lastPathComponent]],
                          NSLocalizedString(@"OK", @""), 
                          nil, 
                          nil);                                     
        }
      NS_ENDHANDLER  
        }
}

- (void)watchedPathDidChange:(NSDictionary *)info
{
}

@end