File: SOGoToolCreateFolder.m

package info (click to toggle)
sogo 5.12.4-1.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 103,828 kB
  • sloc: objc: 122,550; javascript: 75,288; sh: 2,352; ansic: 2,055; perl: 861; python: 777; sql: 307; makefile: 187; php: 43; xml: 27
file content (172 lines) | stat: -rw-r--r-- 4,251 bytes parent folder | download | duplicates (10)
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
/* SOGoToolCreateFolder.m - this file is part of SOGo
 * Implementation of create-folder command for sogo-tool
 *
 * Copyright (C) 2015 Javier Amor Garcia
 *
 * This file 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, or (at your option)
 * any later version.
 *
 * This file 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; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>

#import <SOGo/NSArray+Utilities.h>
#import <SOGo/SOGoObject.h>

#import <GDLContentStore/GCSFolderManager.h>

#import "SOGoToolRestore.h"

@interface SOGoToolCreateFolder : SOGoToolRestore
{
  NSMutableArray *foldersContents;
  NSString *folderType;
}

@end

@implementation SOGoToolCreateFolder

+ (NSString *) command
{
  return @"create-folder";
}

+ (NSString *) description
{
  return @"create folders for a user";
}

- (id) init
{
  if ((self = [super init]))
    {
      foldersContents = nil;
      folderType = nil;
    }

  return self;
}

- (void) dealloc
{
  [foldersContents release];
  [folderType release];
  [super dealloc];
}

- (void) usage
{
  fprintf (stderr, "create-folder user type [displayname ...]\n\n"
           "           user        the user on which the folder(s) will be created\n"
           "           type        type of directory. Calendar or Contacts\n"
           "           displayname display name(s) for the created folder(s)\n\n"
           "Examples:  sogo-tool create-folder user1 Calendar cal1 cal2\n"
           "           sogo-tool create-folder user1 Contacts agenda\n");
}

- (BOOL) createNewFolderOfType: (NSString *) type
                   withContent:  (NSDictionary *) content
{
  NSString *folder;
  NSString *guid;
  GCSFolderManager *fm;
  GCSFolder *gcsFolder;
  BOOL rc;

  guid = [SOGoObject globallyUniqueObjectId];
  folder= [NSString stringWithFormat: @"/Users/%@/%@/%@",
                                      userID, type, guid];
  fm = [GCSFolderManager defaultFolderManager];

  rc = [self createFolder: folder withFM: fm];
  if (!rc)
    {
      NSLog (@"Create directory failed at path %@", folder);
      return NO;
    }

  gcsFolder = [fm folderAtPath: folder];
  if (!gcsFolder)
    {
      NSLog (@"folder '%@' could not be created", folder);
      return NO;
    }

  rc = [self restoreDisplayName: [content objectForKey: @"displayname"]
                       ofFolder: gcsFolder
                         withFM: fm];
  return rc;
}

- (BOOL) proceed
{
  BOOL rc;
  NSUInteger count, i;
  NSDictionary * content;

  rc = YES;
  count = [foldersContents count];
  for (i = 0; i < count; i++)
    {
      content = [foldersContents objectAtIndex: i];
      if (![self createNewFolderOfType: folderType withContent: content])
        {
          rc = NO;
        }
    }

  return rc;
}

- (BOOL) parseArguments
{
  NSString *identifier;
  NSUInteger count, i;
  NSDictionary *content;

  count = [arguments count];
  if (count < 3)
    {
      [self usage];
      return NO;
    }

  identifier = [arguments objectAtIndex: 0];
  if (![self fetchUserID: identifier])
    {
      fprintf (stderr, "Invalid user:%s\n", [identifier cString]);
      return NO;
    }

  folderType = [arguments objectAtIndex: 1];
  if (!([folderType isEqualToString: @"Contacts"] || [folderType isEqualToString: @"Calendar"]))
    {
      fprintf (stderr, "Invalid folder type:%s\n", [folderType cString]);
      return NO;
    }

  foldersContents = [[NSMutableArray alloc] init];
  for (i = 2; i < count; i++)
    {
      content =  [NSDictionary dictionaryWithObject: [arguments objectAtIndex: i]
                                             forKey: @"displayname"];
      [foldersContents addObject: content];
    }

  return YES;
}

@end