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
|
/* FSNFunctions.m
*
* Copyright (C) 2004-2024 Free Software Foundation, Inc.
*
* Author: Enrico Sersale
* Riccardo Mottola <rm@gnu.org>
* Date: March 2004
*
* This file is part of the GNUstep FSNode framework
*
* 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.
*/
#include <math.h>
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <GNUstepBase/GNUstep.h>
#import "FSNFunctions.h"
#import "FSNodeRep.h"
NSString *path_separator(void)
{
static NSString *separator = nil;
if (separator == nil) {
#if defined(__MINGW32__)
separator = @"\\";
#else
separator = @"/";
#endif
}
return separator;
}
/*
* p1 is parent of p2
*/
BOOL isSubpathOfPath(NSString *p1, NSString *p2)
{
int l1 = [p1 length];
int l2 = [p2 length];
if ((l1 > l2) || ([p1 isEqualToString: p2])) {
return NO;
} else if ([[p2 substringToIndex: l1] isEqualToString: p1]) {
if ([[p2 pathComponents] containsObject: [p1 lastPathComponent]]) {
return YES;
}
}
return NO;
}
NSString *subtractFirstPartFromPath(NSString *path, NSString *firstpart)
{
if ([path isEqual: firstpart] == NO) {
return [path substringFromIndex: [path rangeOfString: firstpart].length +1];
}
return path_separator();
}
NSComparisonResult compareWithExtType(id r1, id r2, void *context)
{
FSNInfoType t1 = [(id <FSNodeRep>)r1 nodeInfoShowType];
FSNInfoType t2 = [(id <FSNodeRep>)r2 nodeInfoShowType];
if (t1 == FSNInfoExtendedType) {
if (t2 != FSNInfoExtendedType) {
return NSOrderedDescending;
}
} else {
if (t2 == FSNInfoExtendedType) {
return NSOrderedAscending;
}
}
return NSOrderedSame;
}
#define ONE_KB 1024LLU
#define ONE_MB (ONE_KB * ONE_KB)
#define ONE_GB (ONE_KB * ONE_MB)
#define ONE_TB (ONE_KB * ONE_GB)
NSString *sizeDescription(unsigned long long size)
{
NSString *sizeStr;
if (size == 1)
sizeStr = @"1 byte";
else if (size == 0)
sizeStr = @"0 bytes";
else if (size < (ONE_KB))
sizeStr = [NSString stringWithFormat:@" %ld bytes", (long)size];
else if (size < (ONE_MB))
sizeStr = [NSString stringWithFormat:@" %3.2fKB", ((double)size / (double)(ONE_KB))];
else if (size < (ONE_GB))
sizeStr = [NSString stringWithFormat:@" %3.2fMB", ((double)size / (double)(ONE_MB))];
else if (size < (ONE_TB))
sizeStr = [NSString stringWithFormat:@" %3.2fGB", ((double)size / (double)(ONE_GB))];
else
sizeStr = [NSString stringWithFormat:@" %3.2fTB", ((double)size / (double)(ONE_TB))];
return sizeStr;
}
NSArray *makePathsSelection(NSArray *selnodes)
{
NSMutableArray *selpaths = [NSMutableArray array];
NSUInteger i;
for (i = 0; i < [selnodes count]; i++) {
[selpaths addObject: [[selnodes objectAtIndex: i] path]];
}
return selpaths;
}
double myrintf(double a)
{
return (floor(a + 0.5));
}
/* --- Text Field Editing Error Messages */
void showAlertNoPermission(Class c, NSString *name)
{
NSRunAlertPanel(
NSLocalizedStringFromTableInBundle(@"Error", nil, [NSBundle bundleForClass:c], @""),
[NSString stringWithFormat: @"%@ \"%@\"!\n",
NSLocalizedStringFromTableInBundle(@"You do not have write permission for", nil, [NSBundle bundleForClass:c], @""),
name],
NSLocalizedStringFromTableInBundle(@"Continue", nil, [NSBundle bundleForClass:c], @""),
nil, nil);
}
void showAlertInRecycler(Class c)
{
NSRunAlertPanel(NSLocalizedStringFromTableInBundle(@"Error", nil, [NSBundle bundleForClass:c], @""),
NSLocalizedStringFromTableInBundle(@"You can't rename an object that is in the Recycler", nil, [NSBundle bundleForClass:c], @""),
NSLocalizedStringFromTableInBundle(@"Continue", nil, [NSBundle bundleForClass:c], @"")
, nil, nil);
}
void showAlertInvalidName(Class c)
{
NSLog(@"Class %@ Bundle %@", c, [NSBundle bundleForClass:c]);
NSRunAlertPanel(NSLocalizedStringFromTableInBundle(@"Error", nil, [NSBundle bundleForClass:c], @""),
NSLocalizedStringFromTableInBundle(@"Invalid name", nil, [NSBundle bundleForClass:c], @""),
NSLocalizedStringFromTableInBundle(@"Continue", nil, [NSBundle bundleForClass:c], @""),
nil, nil);
}
NSInteger showAlertExtensionChange(Class c, NSString *extension)
{
NSString *msg;
NSInteger r;
msg = NSLocalizedStringFromTableInBundle(@"Are you sure you want to add the extension", nil, [NSBundle bundleForClass:c], @"");
msg = [msg stringByAppendingFormat: @"\"%@\" ", extension];
msg = [msg stringByAppendingString: NSLocalizedStringFromTableInBundle(@"to the end of the name?", nil, [NSBundle bundleForClass:c], @"")];
msg = [msg stringByAppendingString: NSLocalizedStringFromTableInBundle(@"\nif you make this change, your folder may appear as a single file.", nil, [NSBundle bundleForClass:c], @"")];
r = NSRunAlertPanel(@"", msg,
NSLocalizedStringFromTableInBundle(@"Cancel", nil, [NSBundle bundleForClass:c], @""),
NSLocalizedStringFromTableInBundle(@"OK", nil, [NSBundle bundleForClass:c], @""),
nil);
return r;
}
void showAlertNameInUse(Class c, NSString *newname)
{
NSRunAlertPanel(
NSLocalizedStringFromTableInBundle(@"Error", nil, [NSBundle bundleForClass:c], @""),
[NSString stringWithFormat: @"%@\"%@\" %@ ",
NSLocalizedStringFromTableInBundle(@"The name ", nil, [NSBundle bundleForClass:c], @""),
newname,
NSLocalizedStringFromTableInBundle(@" is already in use!", nil, [NSBundle bundleForClass:c], @"")],
NSLocalizedStringFromTableInBundle(@"Continue", nil, [NSBundle bundleForClass:c], @""), nil, nil);
}
|