File: TShelfFileIcon.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 (724 lines) | stat: -rw-r--r-- 16,636 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/* TShelfFileIcon.m
 *  
 * Copyright (C) 2003-2021 Free Software Foundation, Inc.
 *
 * Authors: Enrico Sersale <enrico@imago.ro>
 *          Riccardo Mottola <rm@gnu.org>
 * Date: November 2021
 *
 * 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>
#import <GNUstepBase/GNUstep.h>

#import "FSNFunctions.h"
#import "GWFunctions.h"
#import "FSNodeRep.h"

#import "TShelfFileIcon.h"
#import "TShelfIconsView.h"
#import "GWorkspace.h"

@implementation TShelfFileIcon

- (void)dealloc
{
  RELEASE (paths);
  RELEASE (hostname);
  RELEASE (node);
  [super dealloc];
}

- (id)initForPaths:(NSArray *)fpaths
       inIconsView:(TShelfIconsView *)aview
{
  self = [super init];
  if (self)
    {
      NSFont *font;
      NSRect hlightRect;
      NSUInteger count;

      fsnodeRep = [FSNodeRep sharedInstance];
      fm = [NSFileManager defaultManager];
      gw = [GWorkspace gworkspace];

      paths = [NSMutableArray new];
      [paths addObjectsFromArray: fpaths];
      tview = aview;
      labelWidth = [tview cellsWidth] - 4;
      font = [NSFont systemFontOfSize: 12];

      count = [paths count];
      if (count == 1)
	{
	  singlepath = YES;
	  ASSIGN (node, [FSNode nodeWithPath: [paths objectAtIndex: 0]]);

	  if ([[node path] isEqual: path_separator()])
	    {
	      ASSIGN (name, [node path]);
	      isRootIcon = YES;
	    }
	  else
	    {
	      ASSIGN (name, [node name]);
	      isRootIcon = NO;
	    }
	}
      else
	{
	  node = nil;
	  singlepath = NO;
	  isRootIcon = NO;
	  name = [[NSString alloc] initWithFormat: @"%lu items", (unsigned long)count];
	}

      if (singlepath)
	ASSIGN (icon, [fsnodeRep iconOfSize: ICON_SIZE forNode: node]);
      else
	ASSIGN (icon, [fsnodeRep multipleSelectionIconOfSize: ICON_SIZE]);

      hlightRect = NSZeroRect;
      hlightRect.size.width = (float)ICON_SIZE / 3 * 4;
      hlightRect.size.height = hlightRect.size.width * [fsnodeRep highlightHeightFactor];
      if ((hlightRect.size.height - ICON_SIZE) < 4)
	{
	  hlightRect.size.height = ICON_SIZE + 4;
	}
      hlightRect = NSIntegralRect(hlightRect);
      ASSIGN (highlightPath, [fsnodeRep highlightPathOfSize: hlightRect.size]);

      if (isRootIcon)
	{
	  NSHost *host = [NSHost currentHost];
	  NSString *hname = [host name];
	  NSRange range = [hname rangeOfString: @"."];

	  if (range.length != 0)
	    {
	      hname = [hname substringToIndex: range.location];
	    }
	  ASSIGN (hostname, hname);
	}
      else
	{
	  hostname = nil;
	}

      namelabel = [NSTextField new];
      [namelabel setFont: font];
      [namelabel setBezeled: NO];
      [namelabel setEditable: NO];
      [namelabel setSelectable: NO];
      [namelabel setAlignment: NSCenterTextAlignment];
      [namelabel setDrawsBackground: NO];
      [namelabel setTextColor: [NSColor controlTextColor]];
      [self setLabelWidth];
      [self setFrame: NSMakeRect(0, 0, 64, 52)];
      [self registerForDraggedTypes: [NSArray arrayWithObjects:
						NSFilenamesPboardType,
					      @"GWLSFolderPboardType",
					      @"GWRemoteFilenamesPboardType",
					      nil]];

      gridIndex = NSNotFound;
      isDragTarget = NO;
      onSelf = NO;
      trectTag = -1;
      minimumLaunchClicks = 2;
    }

  return self;
}

- (id)initForPaths:(NSArray *)fpaths
        atPosition:(NSPoint)pos
       inIconsView:(TShelfIconsView *)aview
{
  self = [self initForPaths: fpaths inIconsView: aview];
  if (self)
    {
      position = NSMakePoint(pos.x, pos.y);
    }
  return self;
}

- (id)initForPaths:(NSArray *)fpaths
	 gridIndex:(NSUInteger)index
       inIconsView:(TShelfIconsView *)aview
{
  self = [self initForPaths: fpaths inIconsView: aview];
  if (self)
    {
      gridIndex = index;
    }
  return self;
}

- (void)setPaths:(NSArray *)fpaths
{
  NSUInteger count;

  RELEASE (paths);
  RELEASE (node);
  paths = [[NSMutableArray alloc] initWithCapacity: 1];
  [paths addObjectsFromArray: fpaths];
  count = [paths count];

  if (count == 1)
    {
      singlepath = YES;

      ASSIGN (node, [FSNode nodeWithPath: [paths objectAtIndex: 0]]);

      if ([[node path] isEqual: path_separator()])
	{
	  ASSIGN (name, [node path]);
	  isRootIcon = YES;
	}
      else
	{
	  ASSIGN (name, [node name]);
	  isRootIcon = NO;
	}

    }
  else
    {
      node = nil;
      singlepath = NO;
      isRootIcon = NO;
      name = [[NSString alloc] initWithFormat: @"%lu items", (unsigned long)count];
    }

  if (singlepath)
    {
      ASSIGN (icon, [fsnodeRep iconOfSize: ICON_SIZE forNode: node]);
    }
  else
    {
      ASSIGN (icon, [fsnodeRep multipleSelectionIconOfSize: ICON_SIZE]);
    }

  if (isRootIcon)
    {
      NSHost *host = [NSHost currentHost];
      NSString *hname = [host name];
      NSRange range = [hname rangeOfString: @"."];

      if (range.length != 0)
	hname = [hname substringToIndex: range.location];
      ASSIGN (hostname, hname);
    }
  else
    {
      RELEASE (hostname);
      hostname = nil;
    }

  [self setLabelWidth];
  [tview setLabelRectOfIcon: self];
}

 - (NSString *)shownName
 {
  return (isRootIcon ? hostname : name);
 }


- (NSArray *)paths
{
  return paths;
}

- (BOOL)isSinglePath
{
  return singlepath;
}

 - (void)renewIcon
 {
   if (singlepath)
     ASSIGN (icon, [fsnodeRep iconOfSize: ICON_SIZE forNode: node]);
   else
     ASSIGN (icon, [fsnodeRep multipleSelectionIconOfSize: ICON_SIZE]);

   [self setNeedsDisplay: YES];
 }

// maybe this can improved with shared super-class code
- (void)setLabelWidth
{
  NSFont *font = [NSFont systemFontOfSize: 12];
  NSRect rect = [namelabel frame];
  NSString *nstr = isRootIcon ? hostname : name;

  labelWidth = [tview cellsWidth] - 8;

  if (isSelected)
    {
      [namelabel setFrame: NSMakeRect(0, 0, [font widthOfString: nstr] + 8, 14)];
      [namelabel setStringValue: nstr];
    }
  else
    {
      int width = (int)[[namelabel font] widthOfString: nstr] + 8;
      if (width > labelWidth)
	{
	  width = labelWidth;
	}
      [namelabel setFrame: NSMakeRect(0, 0, width, 14)];
      [namelabel setStringValue: cutFileLabelText(nstr, namelabel, width - 8)];
    }

  [(NSView *)tview setNeedsDisplayInRect: rect];
}

- (void)mouseUp:(NSEvent *)theEvent
{
  if ([theEvent clickCount] >= minimumLaunchClicks)
    {
      if (locked == NO)
	[tview openCurrentSelection: paths];

      [self unselect];
    }
}

- (void)mouseDown:(NSEvent *)theEvent
{
  unsigned eventmask = NSAlternateKeyMask | NSCommandKeyMask | NSControlKeyMask;

  CHECK_LOCK;

  if ([theEvent clickCount] == 1)
    {
      NSEvent *nextEvent;
      NSPoint location;
      NSSize offset;
      BOOL startdnd = NO;

      if (isSelected == NO)
	{
	  [self select];
	}

      location = [theEvent locationInWindow];

      while (1)
	{
	  nextEvent = [[self window] nextEventMatchingMask:
				       NSLeftMouseUpMask | NSLeftMouseDraggedMask];

	  if ([nextEvent type] == NSLeftMouseUp)
	    {
	      if ([theEvent modifierFlags] & eventmask)
		{
		  [tview setCurrentSelection: paths];
		}
	      // post again, or mouse-up gets eaten
	      [[self window] postEvent: nextEvent atStart: YES];
	      [self unselect];
	      break;

	    }
	  else if ([nextEvent type] == NSLeftMouseDragged)
	    {
	      if (dragDelay < 5)
		{
		  dragDelay++;
		}
	      else
		{
		  NSPoint p = [nextEvent locationInWindow];
		  offset = NSMakeSize(p.x - location.x, p.y - location.y);
		  startdnd = YES;
		  break;
		}
	    }
	}

      if (startdnd)
	{
	  [tview setFocusedIcon: nil];
	  [self startExternalDragOnEvent: theEvent withMouseOffset: offset];
	}
    }
}

@end

@implementation TShelfFileIcon (DraggingSource)

- (void)declareAndSetShapeOnPasteboard:(NSPasteboard *)pb
{
  NSArray *dndtypes = [NSArray arrayWithObject: NSFilenamesPboardType];
  [pb declareTypes: dndtypes owner: nil];
  [pb setPropertyList: paths forType: NSFilenamesPboardType];
}

- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)flag
{
  return NSDragOperationEvery;
}

@end

@implementation TShelfFileIcon (DraggingDestination)

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
  NSPasteboard *pb;
  NSDragOperation sourceDragMask;
  NSArray *sourcePaths;
  NSString *fromPath;
  NSString *buff;
  unsigned i, count;

  CHECK_LOCK_RET (NSDragOperationNone);

  isDragTarget = NO;

  pb = [sender draggingPasteboard];
  sourcePaths = nil;

  if ([[pb types] containsObject: NSFilenamesPboardType])
    {
      sourcePaths = [pb propertyListForType: NSFilenamesPboardType];
    }
  else if ([[pb types] containsObject: @"GWRemoteFilenamesPboardType"])
    {
      if ([node isApplication] == NO)
	{
	  NSData *pbData = [pb dataForType: @"GWRemoteFilenamesPboardType"];
	  NSDictionary *pbDict = [NSUnarchiver unarchiveObjectWithData: pbData];

	  sourcePaths = [pbDict objectForKey: @"paths"];
	}
    }
  else if ([[pb types] containsObject: @"GWLSFolderPboardType"])
    {
      if ([node isApplication] == NO)
	{
	  NSData *pbData = [pb dataForType: @"GWLSFolderPboardType"];
	  NSDictionary *pbDict = [NSUnarchiver unarchiveObjectWithData: pbData];

	  sourcePaths = [pbDict objectForKey: @"paths"];
	}
    }

  if (sourcePaths == nil)
    return NSDragOperationNone;

  if ([paths isEqualToArray: sourcePaths])
    {
      onSelf = YES;
      isDragTarget = YES;
      return NSDragOperationEvery;
    }

  if (node == nil)
    return NSDragOperationNone;

  if ((([node isDirectory] == NO) && ([node isMountPoint] == NO))
      || ([node isPackage] && ([node isApplication] == NO)))
    {
      return NSDragOperationNone;
    }

  count = [sourcePaths count];
  fromPath = [[sourcePaths objectAtIndex: 0] stringByDeletingLastPathComponent];

  if (count == 0)
    return NSDragOperationNone;

  if (([node isWritable] == NO) && ([node isApplication] == NO))
    return NSDragOperationNone;

  if ([[node path] isEqual: fromPath])
    return NSDragOperationNone;

  if ([sourcePaths containsObject: [node path]])
    return NSDragOperationNone;

  buff = [NSString stringWithString: [node path]];
  while (![buff isEqual: path_separator()])
    {
      for (i = 0; i < count; i++)
        {
          if ([buff isEqual: [sourcePaths objectAtIndex: i]])
            return NSDragOperationNone;
        }
      buff = [buff stringByDeletingLastPathComponent];
    }

  if ([node isDirectory] && [node isParentOfPath: fromPath])
    {
      NSArray *subNodes = [node subNodes];

      for (i = 0; i < [subNodes count]; i++)
	{
	  FSNode *nd = [subNodes objectAtIndex: i];

	  if ([nd isDirectory])
	    {
	      int j;

	      for (j = 0; j < count; j++)
		{
		  NSString *fname = [[sourcePaths objectAtIndex: j] lastPathComponent];

		  if ([[nd name] isEqual: fname])
		    {
		      return NSDragOperationNone;
		    }
		}
	    }
	}
    }

  if ([node isApplication])
    {
      for (i = 0; i < count; i++)
	{
	  CREATE_AUTORELEASE_POOL(arp);
	  FSNode *nd = [FSNode nodeWithPath: [sourcePaths objectAtIndex: i]];

	  if (([nd isPlain] == NO) && ([nd isPackage] == NO))
	    {
	      RELEASE (arp);
	      return NSDragOperationNone;
	    }
	  RELEASE (arp);
	}
    }

  isDragTarget = YES;
  forceCopy = NO;

  ASSIGN (icon, [fsnodeRep openFolderIconOfSize: ICON_SIZE forNode: node]);
  [self setNeedsDisplay: YES];

  sourceDragMask = [sender draggingSourceOperationMask];

  if (sourceDragMask == NSDragOperationCopy)
    {
      return ([node isApplication] ? NSDragOperationMove : NSDragOperationCopy);
    }
  else if (sourceDragMask == NSDragOperationLink)
    {
      return ([node isApplication] ? NSDragOperationMove : NSDragOperationLink);
    }
  else
    {
      if ([[NSFileManager defaultManager] isWritableFileAtPath: fromPath]
	  || [node isApplication])
	{
	  return NSDragOperationEvery;
	}
      else if ([node isApplication] == NO)
	{
	  forceCopy = YES;
	  return NSDragOperationCopy;
	}
    }

  return NSDragOperationNone;
}

- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
  NSDragOperation sourceDragMask;

  if (node == nil)
    return NSDragOperationNone;

  if ((isDragTarget == NO) || locked
      || ([node isPackage] && ([node isApplication] == NO)))
    {
      return NSDragOperationNone;
    }

  sourceDragMask = [sender draggingSourceOperationMask];

  if (sourceDragMask == NSDragOperationCopy)
    {
      return ([node isApplication] ? NSDragOperationMove : NSDragOperationCopy);
    }
  else if (sourceDragMask == NSDragOperationLink)
    {
      return ([node isApplication] ? NSDragOperationMove : NSDragOperationLink);
    }
  else
    {
      return forceCopy ? NSDragOperationCopy : NSDragOperationEvery;
    }

  return NSDragOperationNone;
}

- (void)draggingExited:(id <NSDraggingInfo>)sender
{
  if (isDragTarget)
    {
      isDragTarget = NO;
      if (onSelf == NO)
	{
	  if (node)
	    {
	      ASSIGN (icon, [fsnodeRep iconOfSize: ICON_SIZE forNode: node]);
	      [self setNeedsDisplay: YES];
	    }
	}
      onSelf = NO;
    }
}

- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
  return isDragTarget;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
  return YES;
}

- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
  NSPasteboard *pb;
  NSDragOperation sourceDragMask;
  NSArray *sourcePaths;
  NSString *operation, *source;
  NSMutableArray *files;
  NSMutableDictionary *opDict;
  NSUInteger i;

  isDragTarget = NO;

  if (onSelf)
    {
      onSelf = NO;
      return;
    }

  if (node)
    {
      ASSIGN (icon, [fsnodeRep iconOfSize: ICON_SIZE forNode: node]);
      [self setNeedsDisplay: YES];
    }

  sourceDragMask = [sender draggingSourceOperationMask];
  pb = [sender draggingPasteboard];

  if ([node isApplication] == NO)
    {
      if ([[pb types] containsObject: @"GWRemoteFilenamesPboardType"])
	{
	  NSData *pbData = [pb dataForType: @"GWRemoteFilenamesPboardType"];

	  [gw concludeRemoteFilesDragOperation: pbData
				   atLocalPath: [node path]];
	  return;

	}
      else if ([[pb types] containsObject: @"GWLSFolderPboardType"])
	{
	  NSData *pbData = [pb dataForType: @"GWLSFolderPboardType"];

	  [gw lsfolderDragOperation: pbData
		    concludedAtPath: [node path]];
	  return;
	}
    }

  sourcePaths = [pb propertyListForType: NSFilenamesPboardType];

  if ([node isApplication] == NO)
    {
      source = [[sourcePaths objectAtIndex: 0] stringByDeletingLastPathComponent];

      if ([source isEqual: [gw trashPath]])
	{
	  operation = @"GWorkspaceRecycleOutOperation";
	}
      else
	{
	  if (sourceDragMask == NSDragOperationCopy)
	    {
	      operation = NSWorkspaceCopyOperation;
	    }
	  else if (sourceDragMask == NSDragOperationLink)
	    {
	      operation = NSWorkspaceLinkOperation;
	    }
	  else
	    {
	      if ([fm isWritableFileAtPath: source])
		{
		  operation = NSWorkspaceMoveOperation;
		}
	      else
		{
		  operation = NSWorkspaceCopyOperation;
		}
	    }
	}

      files = [NSMutableArray arrayWithCapacity: 1];
      for (i = 0; i < [sourcePaths count]; i++)
	[files addObject: [[sourcePaths objectAtIndex: i] lastPathComponent]];

      opDict = [NSMutableDictionary dictionaryWithCapacity: 4];
      [opDict setObject: operation forKey: @"operation"];
      [opDict setObject: source forKey: @"source"];
      [opDict setObject: [node path] forKey: @"destination"];
      [opDict setObject: files forKey: @"files"];

      [gw performFileOperation: opDict];

    }
  else
    {
      for (i = 0; i < [sourcePaths count]; i++)
	{
	  NSString *path = [sourcePaths objectAtIndex: i];

	  NS_DURING
	    {
	      [[NSWorkspace sharedWorkspace] openFile: path
				      withApplication: [node name]];
	    }
	  NS_HANDLER
	    {
	      NSRunAlertPanel(NSLocalizedString(@"error", @""),
			      [NSString stringWithFormat: @"%@ %@!",
					NSLocalizedString(@"Can't open ", @""), [node name]],
			      NSLocalizedString(@"OK", @""),
			      nil,
			      nil);
	    }
	  NS_ENDHANDLER
	    }
    }
}

@end