File: UtilityFunctions.h

package info (click to toggle)
projectmanager.app 0.2-3.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,812 kB
  • ctags: 306
  • sloc: objc: 19,267; makefile: 77; sh: 64
file content (275 lines) | stat: -rw-r--r-- 8,512 bytes parent folder | download | duplicates (3)
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
/*
    UtilityFunctions.h

    Utility static functions for the FileManager class of ProjectManager.

    Copyright (C) 2005  Saso Kiselkov

    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 St, Fifth Floor, Boston, MA  02110-1301  USA
*/

#import <Foundation/NSError.h>
#import <Foundation/NSString.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSFileManager.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSException.h>
#import <Foundation/NSValue.h>

#import <AppKit/NSPanel.h>
#import <AppKit/NSTextField.h>

#import "FileManager.h"

/**
 * This function pops up an alert panel describing an NSError object. The
 * description of an error is taken from it's NSLocalizedDescriptionKey
 * userInfo entry.
 *
 * @param error The error which to describe.
 * @param aTitle The title of the alert panel.
 * @param aDescription An additional description of the error. If specified,
 *      this argument is printed out in the alert panel's main text area,
 *      followed by a colon and a description of the NSError object. If it
 *      is `nil', only the error's description is printed in the panel.
 * @param firstButton The title of the first button. If nothing is specified,
 *      this defaults to "OK".
 * @param secondButton The title of the second button. If nothing is
 *      specified, this button is ommited.
 * @param thirdButton The title of the third button. If nothing is specified,
 *      this button is ommited.
 *
 * @return The code of the clicked button to dismiss the panel, as returned
 *      by NSRunAlertPanel.
 */
static int
DescribeError (NSError * error,
               NSString * aTitle,
               NSString * aDescription,
               NSString * firstButton,
               NSString * secondButton,
               NSString * thirdButton,
               ...)
{
  NSString * description;
  NSString * prefix, * errorDescription;

  if (aDescription == nil)
    {
      prefix = @"";
    }
  else
    {
      va_list arglist;

      va_start (arglist, thirdButton);
      prefix = [NSString stringWithFormat: aDescription arguments: arglist];
      va_end (arglist);

      prefix = [prefix stringByAppendingString: _(@": ")];
    }

  errorDescription = [[error userInfo] objectForKey:
    NSLocalizedDescriptionKey];

  description = [NSString stringWithFormat: @"%@%@", prefix, errorDescription];

  return NSRunAlertPanel (aTitle,
                          description,
                          firstButton,
                          secondButton,
                          thirdButton);
}

/**
 * This function is a shorthand for creating NSError objects and setting
 * error object pointers in the FileManager class code. The NSError
 * object's error domain is always set ProjectFilesErrorDomain.
 *
 * @param ptr A pointer to an area which is to be filled with the NSError
 *      object. In case the passed pointer is NULL, this function immediately
 *      returns and doesn't create any NSError object.
 * @param code The code of the error.
 * @param reasonFormat A format string specifying the reason of the error.
 *      This will be put into the error's userInfo dictionary under the
 *      NSLocalizedDescriptionKey. The remaining variable arguments are
 *      arguments for the format string.
 */
static void
SetFileError (NSError ** ptr, int code, NSString * reasonFormat, ...)
{
  if (ptr != NULL)
    {
      NSString * reason;
      NSDictionary * userInfo;
      va_list arglist;

      va_start (arglist, reasonFormat);
      reason = [[[NSString alloc]
        initWithFormat: reasonFormat arguments: arglist]
        autorelease];
      va_end (arglist);

      userInfo = [NSDictionary
        dictionaryWithObject: reason forKey: NSLocalizedDescriptionKey];
      *ptr = [NSError errorWithDomain: ProjectFilesErrorDomain
                                 code: code
                             userInfo: userInfo];
    }
}

/**
 * Posts a ProjectFilesDidChangeNotification to the default notification
 * center. The notification's user info will be structured like this:
 * {
 *   Project = "<path-to-project-file>";
 *   Category = "<category-who's-contents-changed>";
 * }
 *
 * @param sender The sender FileManager of the notification.
 * @param category The category which will be declared in the user info
 *      under the `Category' key. Passing `nil' will result in the
 *      `Category' key not being present in the user info dictionary.
 */
static void
PostFilesChangedNotification (FileManager * sender, NSString * category)
{
  NSDictionary * userInfo;
  NSString * projectPath;
  static NSNotificationCenter * nc = nil;

  if (nc == nil)
    {
      nc = [NSNotificationCenter defaultCenter];
    }

  projectPath = [[sender document] fileName];
  if (category != nil)
    {
      userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
        projectPath, @"Project",
        category, @"Category",
        nil];
    }
  else
    {
      userInfo = [NSDictionary dictionaryWithObject: projectPath
                                             forKey: @"Project"];
    }

  [nc postNotificationName: ProjectFilesDidChangeNotification
                    object: sender
                  userInfo: userInfo];
}


/**
 * Sets the text field `tf' to editable or non-editable, based on `flag'.
 * If the text field is set to editable, it's also made to draw it's
 * background and it's target is set to `target'. Otherwise, it's made
 * to not display it's background and it's target is reset.
 */
static inline void
SetTextFieldEnabled (NSTextField * tf, BOOL flag)
{
  if ([tf isEnabled] != flag)
    {
      [tf setEnabled: flag];
      [tf setEditable: flag];
      [tf setSelectable: flag];
      [tf setDrawsBackground: flag];
    }
}

/**
 * Constructs a human-readable description of the data size `size'.
 * E.g. if the value is greater than 1024, then the prefix 'kB' is
 * appended to indicate kilobytes and the number is represented as
 * a fraction of this unit.
 *
 * @return The resulting string.
 */
static inline NSString *
MakeSizeStringFromValue (unsigned long long size)
{
  if (size < 1024)
    {
      return [NSString stringWithFormat: _(@"%i bytes"), size];
    }
  else if (size < 1024 * 1024)
    {
      return [NSString stringWithFormat: _(@"%.2f kB"), (double) size / 1024];
    }
  else if (size < 1024 * 1024 * 1024)
    {
      return [NSString stringWithFormat: _(@"%.2f MB"), (double) size /
      (1024 * 1024)];
    }
  else
    {
      return [NSString stringWithFormat: _(@"%.2f GB"),
        (double) size / (1024 * 1024 * 1024)];
    }
}

/**
 * This function translates `linkTarget', which was the target of a
 * link originally located at `oldLocation', to correctly point to
 * it's target from `newLocation'.
 *
 * @return The translocated link target.
 */
static inline NSString *
TranslocateLinkTarget (NSString * linkTarget, NSString * oldLocation,
                       NSString * newLocation)
{
  // absolute paths do not need to be translocated
  if ([linkTarget isAbsolutePath])
    {
      return linkTarget;
    }
  else
    {
      return [newLocation stringByConstructingRelativePathTo:
        [oldLocation stringByConcatenatingWithPath: linkTarget]];
    }
}

/**
 * Removes the directory (and any superdirectories of it) at `aPath'
 * which are empty.
 */
static BOOL
PurgeUnneededDirectories (NSString * aPath, NSError ** error)
{
  NSFileManager * fm = [NSFileManager defaultManager];

  for (;
       [[fm directoryContentsAtPath: aPath] count] == 0;
       aPath = [aPath stringByDeletingLastPathComponent])
    {
      if (![fm removeFileAtPath: aPath handler: nil])
        {
          SetFileError (error, ProjectFilesDeletionError,
            _(@"Couldn't delete directory at path %@."), aPath);

          return NO;
        }
    }

  return YES;
}