File: GWFunctions.m

package info (click to toggle)
gworkspace 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 20,832 kB
  • sloc: objc: 69,382; sh: 488; makefile: 39
file content (302 lines) | stat: -rw-r--r-- 7,134 bytes parent folder | download | duplicates (2)
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
/* GWFunctions.m
 *  
 * Copyright (C) 2003-2013 Free Software Foundation, Inc.
 *
 * Author: Enrico Sersale <enrico@imago.ro>
 * Date: August 2001
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111 USA.
 */

#include <math.h>
#include <limits.h>

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


#define ONE_KB 1024
#define ONE_MB (ONE_KB * ONE_KB)
#define ONE_GB (ONE_KB * ONE_MB)

#define ATTRIBUTES_AT_PATH(a, p, l) \
a = [[NSFileManager defaultManager] fileAttributesAtPath: (NSString *)p traverseLink: l]

#define SORT_INDEX(i, p) { \
BOOL isdir; \
[[NSFileManager defaultManager] fileExistsAtPath: (NSString *)p isDirectory: &isdir]; \
if (isdir) { \
i = 2; \
} else { \
if ([[NSFileManager defaultManager] isExecutableFileAtPath: (NSString *)p] == YES) { \
i = 1; \
} else { \
i = 0; \
} \
} }

#define byname 0
#define bykind 1
#define bydate 2
#define bysize 3
#define byowner 4

static NSString *dots = @"...";
static float dtslenght = 0.0;  
static NSFont *lablfont = nil;
static NSDictionary *fontAttr = nil;

@protocol IconsProtocol

- (NSString *)myName;

@end 

NSString *systemRoot()
{
  static NSString *root = nil;

  if (root == nil) {
    #if defined(__MINGW32__)
    /* FIXME !!!!!! */
      root = @"\\";	
    #else
      root = @"/";	
    #endif

    RETAIN (root);
  }

  return root;
}

NSString *cutFileLabelText(NSString *filename, id label, int lenght)
{
	if (lenght > 0) {
	  NSFont *font = [label font];
  
    if ((lablfont == nil) || ([lablfont isEqual: font] == NO)) {
      ASSIGN (lablfont, font);
      ASSIGN (fontAttr, [NSDictionary dictionaryWithObject: lablfont 
                                                    forKey: NSFontAttributeName]);
      dtslenght = [dots sizeWithAttributes: fontAttr].width;     
    }

    if ([filename sizeWithAttributes: fontAttr].width > lenght) {
      int tl = [filename length];

      if (tl <= 5) {
        return dots;
      } else {
        int fpto = (tl / 2) - 2;
        int spfr = fpto + 3;
        NSString *fp = [filename substringToIndex: fpto];
        NSString *sp = [filename substringFromIndex: spfr];
        NSString *dotted = [NSString stringWithFormat: @"%@%@%@", fp, dots, sp];
        int dl = [dotted length];
        float dotl = [dotted sizeWithAttributes: fontAttr].width;
        int p = 0;

        while (dotl > lenght) {
          if (dl <= 5) {
            return dots;
          }        

          if (p) {
            fpto--;
          } else {
            spfr++;
          }
          p = !p;

          fp = [filename substringToIndex: fpto];
          sp = [filename substringFromIndex: spfr];
          dotted = [NSString stringWithFormat: @"%@%@%@", fp, dots, sp];
          dotl = [dotted sizeWithAttributes: fontAttr].width;
          dl = [dotted length];
        }      

        return dotted;
      }
    }

    return filename;
	}
  
	return filename;
}

BOOL subPathOfPath(NSString *p1, NSString *p2)
{
  int l1 = [p1 length];
  int l2 = [p2 length];  

  if ((l1 > l2) || ([p1 isEqual: p2])) {
    return NO;
  } else if ([[p2 substringToIndex: l1] isEqual: p1]) {
    if ([[p2 pathComponents] containsObject: [p1 lastPathComponent]]) {
      return YES;
    }
  }

  return NO;
}

NSString *pathRemovingPrefix(NSString *path, NSString *prefix)
{
  if ([path hasPrefix: prefix]) {
	  return [path substringFromIndex: [path rangeOfString: prefix].length + 1];
  }

  return path;  	
}

NSString *commonPrefixInArray(NSArray *a)
{
  NSString *s = @"";
  unsigned minlngt = INT_MAX;
  int index = 0;
  BOOL done = NO;
  int i, j;
  
  if ([a count] == 0) {
    return nil;
  }
  if ([a count] == 1) {
    return [a objectAtIndex: 0];
  }
  
  for (i = 0; i < [a count]; i++) {
    unsigned l = [[a objectAtIndex: i] length];
    if (l < minlngt) {
      minlngt = l;
    }
  }
  
  while (index < minlngt) {
    NSString *s1, *s2;
    unichar c1, c2;

    s1 = s2 = nil;
    
    for (i = 0; i < [a count]; i++) {
      s1 = [a objectAtIndex: i];
      c1 = [s1 characterAtIndex: index];

      for (j = 0; j < [a count]; j++) {
        s2 = [a objectAtIndex: j];
        c2 = [s2 characterAtIndex: index];

        if (i != j) {
          if (c1 != c2) {
            done = YES;
            break;
          }
        }
      }
    
      if (done) {
        break;
      }
    } 

    if (done) {
      break;
    }
    
    s = [s1 substringWithRange: NSMakeRange(0, index + 1)];
       
    index++;
  } 
  
  if ([s length]) {
    return s;
  }
  
  return nil;
}

NSString *fileSizeDescription(unsigned long long size)
{
  NSString *sizeStr;
  char *sign = "";
    
  if(size == 1)
    sizeStr = @"1 byte";
  else if(size == 0)
    sizeStr = @"0 bytes";
  else if(size < (10 * ONE_KB))
    sizeStr = [NSString stringWithFormat:@"%s %ld bytes", sign, (long)size];
  else if(size < (100 * ONE_KB))
    sizeStr = [NSString stringWithFormat:@"%s %3.2fKB", sign,
			((double)size / (double)(ONE_KB))];
  else if(size < (100 * ONE_MB))
    sizeStr = [NSString stringWithFormat:@"%s %3.2fMB", sign,
			((double)size / (double)(ONE_MB))];
  else
    sizeStr = [NSString stringWithFormat:@"%s %3.2fGB", sign,
			((double)size / (double)(ONE_GB))];

  return sizeStr;
}

NSRect rectForWindow(NSArray *otherwins, NSRect proposedRect, BOOL checkKey)
{
  NSRect scr = [[NSScreen mainScreen] visibleFrame];
  NSRect wr = proposedRect;
  int margin = 50;
  int shift = 100;
  NSPoint p = wr.origin;
  int i;  

	for (i = [otherwins count] - 1; i >= 0; i--) {
    NSWindow *window = [otherwins objectAtIndex: i];

    if ([window isKeyWindow] || (checkKey == NO)) {
      p = [window frame].origin;
      p.x += shift;
      p.y -= shift;
      p.y = (p.y < margin) ? margin : p.y;
      if ((p.x + proposedRect.size.width) > (scr.size.width - margin)) {
        p.x -= (shift * 2);
      }
      wr.origin = p;
    }
  }

	for (i = 0; i < [otherwins count]; i++) {
    NSRect r = [[otherwins objectAtIndex: i] frame];

    if (NSEqualRects(wr, r)) {
      p.x += shift;
      p.y -= shift;
      p.y = (p.y < margin) ? margin : p.y;
      if ((p.x + proposedRect.size.width) > (scr.size.width - margin)) {
        p.x -= (shift * 2);
      }
      wr.origin = p;
    }
  }
  
  if (NSEqualRects(wr, proposedRect)) {
    wr.origin.x = scr.origin.x + shift;
    wr.origin.y = scr.size.height - wr.size.height - shift;
  }  
  
  return NSIntegralRect(wr);
}