• R/O
  • HTTP
  • SSH
  • HTTPS

Tombo: Commit

TOMBO source code


Commit MetaInfo

Revisionfc499ae17bad98546576a58804f90079d557ba5e (tree)
Time2012-03-31 18:12:52
AuthorHirami <tomohisa.hirami@nift...>
CommiterHirami

Log Message

When renaming and exists same name file, add '(n)' to file name and title.

Change Summary

Incremental Difference

--- a/iOS/Tombo/Tombo/Storage.m
+++ b/iOS/Tombo/Tombo/Storage.m
@@ -62,9 +62,7 @@
6262 // save note
6363 -(FileItem *)save:(NSString *)note item:(FileItem *)item {
6464 if (!item) return nil;
65-
66- FileItem *result = [FileItem alloc];
67-
65+
6866 // Decide new title.
6967 NSRange r;
7068 r.location = 0;
@@ -77,23 +75,21 @@
7775 if (title.length == 0) {
7876 title = @"New document";
7977 }
80-
78+
8179 // 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+ }
9389 } else {
9490 result = item;
9591 }
96-
92+
9793 // Save note.
9894 NSError *error = nil;
9995 [note writeToFile:result.path atomically:YES encoding:NSUTF8StringEncoding error:&error];
@@ -101,6 +97,45 @@
10197 return result;
10298 }
10399
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+
104139 - (FileItem *)newItem {
105140 FileItem *p = [FileItem allocWithName: nil];
106141 p.path = [documentRoot stringByAppendingString:@"/_dummy.txt"];
Show on old repository browser