ProjectManager 0.2
UtilityFunctions.h
1/*
2 UtilityFunctions.h
3
4 Utility static functions for the FileManager class of ProjectManager.
5
6 Copyright (C) 2005 Saso Kiselkov
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21*/
22
23#import <Foundation/NSError.h>
24#import <Foundation/NSString.h>
25#import <Foundation/NSArray.h>
26#import <Foundation/NSBundle.h>
27#import <Foundation/NSFileManager.h>
28#import <Foundation/NSNotification.h>
29#import <Foundation/NSUserDefaults.h>
30#import <Foundation/NSException.h>
31#import <Foundation/NSValue.h>
32
33#import <AppKit/NSPanel.h>
34#import <AppKit/NSTextField.h>
35
36#import "FileManager.h"
37
59static int
60DescribeError (NSError * error,
61 NSString * aTitle,
62 NSString * aDescription,
63 NSString * firstButton,
64 NSString * secondButton,
65 NSString * thirdButton,
66 ...)
67{
68 NSString * description;
69 NSString * prefix, * errorDescription;
70
71 if (aDescription == nil)
72 {
73 prefix = @"";
74 }
75 else
76 {
77 va_list arglist;
78
79 va_start (arglist, thirdButton);
80 prefix = [NSString stringWithFormat: aDescription arguments: arglist];
81 va_end (arglist);
82
83 prefix = [prefix stringByAppendingString: _(@": ")];
84 }
85
86 errorDescription = [[error userInfo] objectForKey:
87 NSLocalizedDescriptionKey];
88
89 description = [NSString stringWithFormat: @"%@%@", prefix, errorDescription];
90
91 return NSRunAlertPanel (aTitle,
92 description,
93 firstButton,
94 secondButton,
95 thirdButton);
96}
97
112static void
113SetFileError (NSError ** ptr, int code, NSString * reasonFormat, ...)
114{
115 if (ptr != NULL)
116 {
117 NSString * reason;
118 NSDictionary * userInfo;
119 va_list arglist;
120
121 va_start (arglist, reasonFormat);
122 reason = [[[NSString alloc]
123 initWithFormat: reasonFormat arguments: arglist]
124 autorelease];
125 va_end (arglist);
126
127 userInfo = [NSDictionary
128 dictionaryWithObject: reason forKey: NSLocalizedDescriptionKey];
129 *ptr = [NSError errorWithDomain: ProjectFilesErrorDomain
130 code: code
131 userInfo: userInfo];
132 }
133}
134
148static void
149PostFilesChangedNotification (FileManager * sender, NSString * category)
150{
151 NSDictionary * userInfo;
152 NSString * projectPath;
153 static NSNotificationCenter * nc = nil;
154
155 if (nc == nil)
156 {
157 nc = [NSNotificationCenter defaultCenter];
158 }
159
160 projectPath = [[sender document] fileName];
161 if (category != nil)
162 {
163 userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
164 projectPath, @"Project",
165 category, @"Category",
166 nil];
167 }
168 else
169 {
170 userInfo = [NSDictionary dictionaryWithObject: projectPath
171 forKey: @"Project"];
172 }
173
174 [nc postNotificationName: ProjectFilesDidChangeNotification
175 object: sender
176 userInfo: userInfo];
177}
178
179
186static inline void
187SetTextFieldEnabled (NSTextField * tf, BOOL flag)
188{
189 if ([tf isEnabled] != flag)
190 {
191 [tf setEnabled: flag];
192 [tf setEditable: flag];
193 [tf setSelectable: flag];
194 [tf setDrawsBackground: flag];
195 }
196}
197
206static inline NSString *
207MakeSizeStringFromValue (unsigned long long size)
208{
209 if (size < 1024)
210 {
211 return [NSString stringWithFormat: _(@"%i bytes"), size];
212 }
213 else if (size < 1024 * 1024)
214 {
215 return [NSString stringWithFormat: _(@"%.2f kB"), (double) size / 1024];
216 }
217 else if (size < 1024 * 1024 * 1024)
218 {
219 return [NSString stringWithFormat: _(@"%.2f MB"), (double) size /
220 (1024 * 1024)];
221 }
222 else
223 {
224 return [NSString stringWithFormat: _(@"%.2f GB"),
225 (double) size / (1024 * 1024 * 1024)];
226 }
227}
228
236static inline NSString *
237TranslocateLinkTarget (NSString * linkTarget, NSString * oldLocation,
238 NSString * newLocation)
239{
240 // absolute paths do not need to be translocated
241 if ([linkTarget isAbsolutePath])
242 {
243 return linkTarget;
244 }
245 else
246 {
247 return [newLocation stringByConstructingRelativePathTo:
248 [oldLocation stringByConcatenatingWithPath: linkTarget]];
249 }
250}
251
256static BOOL
257PurgeUnneededDirectories (NSString * aPath, NSError ** error)
258{
259 NSFileManager * fm = [NSFileManager defaultManager];
260
261 for (;
262 [[fm directoryContentsAtPath: aPath] count] == 0;
263 aPath = [aPath stringByDeletingLastPathComponent])
264 {
265 if (![fm removeFileAtPath: aPath handler: nil])
266 {
267 SetFileError (error, ProjectFilesDeletionError,
268 _(@"Couldn't delete directory at path %@."), aPath);
269
270 return NO;
271 }
272 }
273
274 return YES;
275}
Definition FileManager.h:94