TOMBO source code
Revision | fc499ae17bad98546576a58804f90079d557ba5e (tree) |
---|---|
Time | 2012-03-31 18:12:52 |
Author | Hirami <tomohisa.hirami@nift...> |
Commiter | Hirami |
When renaming and exists same name file, add '(n)' to file name and title.
@@ -62,9 +62,7 @@ | ||
62 | 62 | // save note |
63 | 63 | -(FileItem *)save:(NSString *)note item:(FileItem *)item { |
64 | 64 | if (!item) return nil; |
65 | - | |
66 | - FileItem *result = [FileItem alloc]; | |
67 | - | |
65 | + | |
68 | 66 | // Decide new title. |
69 | 67 | NSRange r; |
70 | 68 | r.location = 0; |
@@ -77,23 +75,21 @@ | ||
77 | 75 | if (title.length == 0) { |
78 | 76 | title = @"New document"; |
79 | 77 | } |
80 | - | |
78 | + | |
81 | 79 | // If title is changed, rename one. |
82 | - if (!item.name) { | |
83 | - // New item. | |
84 | - result.name = title; | |
85 | - result.path = [[item.path stringByDeletingLastPathComponent] stringByAppendingFormat:@"/%@.%@", title, [item.path pathExtension]]; | |
86 | - } else if (![title isEqualToString: item.name]) { | |
87 | - // Title is changed. Rename one. | |
88 | - NSString *toPath = [[item.path stringByDeletingLastPathComponent] stringByAppendingFormat:@"/%@.%@", title, [item.path pathExtension]]; | |
89 | - NSError *error = nil; | |
90 | - [fileManager moveItemAtPath:item.path toPath:toPath error:&error]; | |
91 | - result.name = title; | |
92 | - result.path = toPath; | |
80 | + FileItem *result; | |
81 | + if (!item.name || ![title isEqualToString: item.name]) { | |
82 | + result = [self decideFileName:title path:item.path]; | |
83 | + | |
84 | + if (item.name) { | |
85 | + // If it is not new item, needs rename. | |
86 | + NSError *error = nil; | |
87 | + [fileManager moveItemAtPath:item.path toPath:result.path error:&error]; | |
88 | + } | |
93 | 89 | } else { |
94 | 90 | result = item; |
95 | 91 | } |
96 | - | |
92 | + | |
97 | 93 | // Save note. |
98 | 94 | NSError *error = nil; |
99 | 95 | [note writeToFile:result.path atomically:YES encoding:NSUTF8StringEncoding error:&error]; |
@@ -101,6 +97,45 @@ | ||
101 | 97 | return result; |
102 | 98 | } |
103 | 99 | |
100 | +- (FileItem *)decideFileName:(NSString *)titleCand path:(NSString *)origPath { | |
101 | + FileItem *result = [FileItem alloc]; | |
102 | + | |
103 | + NSString *ext = [origPath pathExtension]; | |
104 | + | |
105 | + NSMutableString *path = [NSMutableString stringWithCapacity:256]; | |
106 | + [path appendString:[origPath stringByDeletingLastPathComponent]]; | |
107 | + [path appendString:@"/"]; | |
108 | + [path appendString:titleCand]; | |
109 | + NSUInteger n = [path length]; | |
110 | + [path appendString:@"."]; | |
111 | + [path appendString:ext]; | |
112 | + NSUInteger u = [path length]; | |
113 | + | |
114 | + if ([fileManager fileExistsAtPath:path]) { | |
115 | + NSUInteger cnt = 1; | |
116 | + while(YES) { | |
117 | + NSRange r; | |
118 | + r.location = n; | |
119 | + r.length = u - n; | |
120 | + [path deleteCharactersInRange:r]; | |
121 | + [path appendFormat:@"(%d)", cnt]; | |
122 | + [path appendString:@"."]; | |
123 | + [path appendString:ext]; | |
124 | + u = [path length]; | |
125 | + | |
126 | + if (![fileManager fileExistsAtPath:path]) break; | |
127 | + cnt++; | |
128 | + } | |
129 | + | |
130 | + result.name = [[path lastPathComponent] stringByDeletingPathExtension]; | |
131 | + } else { | |
132 | + result.name = titleCand; | |
133 | + } | |
134 | + result.path = path; | |
135 | + | |
136 | + return result; | |
137 | +} | |
138 | + | |
104 | 139 | - (FileItem *)newItem { |
105 | 140 | FileItem *p = [FileItem allocWithName: nil]; |
106 | 141 | p.path = [documentRoot stringByAppendingString:@"/_dummy.txt"]; |