File: Util.m

package info (click to toggle)
poe.app 0.5.1-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 564 kB
  • sloc: objc: 1,272; ansic: 438; makefile: 54; sh: 17
file content (316 lines) | stat: -rw-r--r-- 8,414 bytes parent folder | download | duplicates (4)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
    Util.m - Class to manage the 'standard' vorbis comment 
    fields for Poe.app
    Copyright (C) 2003,2004,2005 Rob Burns

    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., 675 Mass Ave, Cambridge, MA 02111, USA.
*/

#include "Util.h"

static Util *singleInstance = nil;

@implementation Util 

- (void) dealloc
{
  RELEASE(_tags);
  RELEASE(_tagsTitle);
  RELEASE(_tagsDescription);
  RELEASE(_genres);
 
  [[NSNotificationCenter defaultCenter] removeObserver: self];

  [super dealloc];
}

- (id) init
{
  int i;
  NSDictionary *standardCommentInfo;
  NSMutableArray *tempArray;

  self = [super init];

  NSString *path = [[NSBundle mainBundle] pathForResource: @"CommentInfo" ofType: @"plist"];
  standardCommentInfo = [NSDictionary dictionaryWithContentsOfFile: path];

  tempArray = [NSMutableArray arrayWithArray:
    [standardCommentInfo objectForKey: @"tagsTitle"]];

  for(i=0;i<[tempArray count];i++)
    {
      [tempArray replaceObjectAtIndex: i withObject:
        NSLocalizedStringFromTable([tempArray objectAtIndex: i], @"CommentInfo.strings",nil)];
    }

  _tagsTitle = [[NSArray alloc] initWithArray: tempArray];

  tempArray = [NSMutableArray arrayWithArray:
    [standardCommentInfo objectForKey: @"tagsDescription"]];

  for(i=0;i<[tempArray count];i++)
    {
      [tempArray replaceObjectAtIndex: i withObject:
        NSLocalizedStringFromTable([tempArray objectAtIndex: i], @"CommentInfo.strings",nil)];
    }

  _tagsDescription = [[NSArray alloc] initWithArray: tempArray];

  _tags = [[NSArray alloc] initWithArray:
    [standardCommentInfo objectForKey: @"tagsWritten"]];

  _genres = [[NSMutableArray alloc] initWithArray:
    [standardCommentInfo objectForKey: @"genres"]];

  [self syncGenres: nil];

  [[NSNotificationCenter defaultCenter] addObserver: self
                                           selector: @selector(syncGenres:)
                                               name: @"GenresDidChange"
                                             object: nil];

  return self;
}

+ (id) singleInstance
{
  if( !singleInstance )
    {
      singleInstance = [[Util alloc] init];
    }

  return singleInstance;
}

- (void) syncGenres: (NSNotification *)not
{
  NSArray *gAdded;
  NSArray *gRemoved;
  int i;

  gAdded = [[NSUserDefaults standardUserDefaults] arrayForKey: @"genresAdded"];
  gRemoved = [[NSUserDefaults standardUserDefaults] arrayForKey: @"genresRemoved"];

  for(i=0;i<[gAdded count];i++)
    {
      if(![_genres containsObject: [gAdded objectAtIndex: i]])
        {
          [_genres addObject: [gAdded objectAtIndex: i]];
        }
    }

  for(i=0;i<[gRemoved count];i++)
    {
      if([_genres containsObject: [gRemoved objectAtIndex: i]])
        {
          int index = [_genres indexOfObject: [gRemoved objectAtIndex: i]];
          [_genres removeObjectAtIndex: index];
        }
    }
}

- (NSArray *)tags
{
  return _tags;
}

- (NSArray *)tagsTitle
{
  return _tagsTitle;
}

- (NSArray *)tagsDescription;
{
  return _tagsDescription;
}

- (NSArray *)genres
{
  return _genres;
}

- (NSString *) tagAtIndex: (int)index
{
  return [_tags objectAtIndex: index];
}

- (NSString *) titleAtIndex: (int)index
{
  return [_tagsTitle objectAtIndex: index];
}

- (NSString *) descriptionAtIndex: (int)index
{
  return [_tagsDescription objectAtIndex: index];
}

- (int) indexOfTag: (NSString *) tag
{
  int i;

  for(i=0;i<[_tags count];i++)
    {
      if([[_tags objectAtIndex: i] isEqualToString: tag])
        {
          return i;
        }
    }
  return -1;
}

- (int) indexOfTitle: (NSString *) title
{
  int i;

  for(i=0;i<[_tagsTitle count];i++)
    {
      if([[_tagsTitle objectAtIndex: i] isEqualToString: title])
        {
          return i;
        }
    }
  return -1;
}

- (NSArray *) tagTemplate
{
  NSMutableArray *template;
  NSArray *def;
  NSMutableDictionary *temp;
  int i;

  template = [NSMutableArray new];
  def = [[NSUserDefaults standardUserDefaults] arrayForKey: @"defaultTags"];

  for(i=0;i<[_tags count];i++)
    {
      temp = [NSMutableDictionary new];
      [temp setObject: [_tagsTitle objectAtIndex: i] forKey: @"title"];
      [temp setObject: [_tags objectAtIndex: i] forKey: @"tag"];
  
      if([def containsObject: [_tags objectAtIndex: i]])
        {
          [temp setObject: @"YES"  forKey: @"default"];
        }
      else
        {
          [temp setObject: @"NO" forKey: @"default"];
        }

      [template addObject: temp];
    }

  return [NSArray arrayWithArray: template];
}

- (NSArray*) formatComments: (NSArray *)arrayIn
{
  NSMutableDictionary *temp;
  NSMutableArray *arrayOut;
  int scount;
  int i,k,j;

  arrayOut = [NSMutableArray arrayWithArray:
    [self tagTemplate]];

  scount = [arrayOut count]-1;

  for(i=0;i<[arrayIn count];i++)
    {
      for(k=0;k<[arrayOut count];k++)
        {
          if([[[arrayOut objectAtIndex: k] objectForKey: @"tag"]
            isEqualToString: [[arrayIn objectAtIndex: i] objectForKey: @"tag"]] &&
            [[arrayOut objectAtIndex: k] objectForKey: @"value"] == nil)
            {
              if(![[arrayIn objectAtIndex: i] objectForKey: @"value"])
                {
                  [[arrayOut objectAtIndex: k] setObject: @""
                    forKey: @"value"];
                }
              else
                {
                  [[arrayOut objectAtIndex: k] setObject:
                    [[arrayIn objectAtIndex: i] objectForKey: @"value"]
                    forKey: @"value"];
                }

              break;
            }
          else if([[[arrayOut objectAtIndex: k] objectForKey: @"tag"]
            isEqualToString: [[arrayIn objectAtIndex: i] objectForKey: @"tag"]] &&
            [[arrayOut objectAtIndex: k] objectForKey: @"value"] != nil)
            {
              for(j=k;j<[arrayOut count];j++)
                {
                  if(![[[arrayOut objectAtIndex: j] objectForKey: @"tag"] isEqualToString:
                  [[arrayIn objectAtIndex: i] objectForKey: @"tag"]])
                    {
                      break;
                    }
                }

              if(![[arrayIn objectAtIndex: i] objectForKey: @"value"])
                {
                  temp = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                    [[arrayOut objectAtIndex: k] objectForKey: @"title"], @"title",
                    [[arrayOut objectAtIndex: k] objectForKey: @"tag"], @"tag",
                    nil];
                }
              else
                {
                  temp = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                    [[arrayIn objectAtIndex: i] objectForKey: @"value"], @"value",
                    [[arrayOut objectAtIndex: k] objectForKey: @"title"], @"title",
                    [[arrayOut objectAtIndex: k] objectForKey: @"tag"], @"tag",
                    nil];
                }

              [arrayOut insertObject: temp atIndex: j];

              break;
            }
          else if(k == scount)
            {
              temp = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                [[arrayIn objectAtIndex: i] objectForKey: @"value"], @"value",
                [[arrayIn objectAtIndex: i] objectForKey: @"tag"], @"title",
                [[arrayIn objectAtIndex: i] objectForKey: @"tag"], @"tag",
                @"NO",@"default",
                nil];

              [arrayOut addObject: temp];

              break;
            }
        }
    }

  for(i=0;i<[arrayOut count];i++)
    {
      if([[arrayOut objectAtIndex: i] objectForKey: @"value"] == nil &&
        [[[arrayOut objectAtIndex: i] objectForKey: @"default"] isEqualToString: @"NO"])
        {
          [arrayOut removeObjectAtIndex: i];
          i--;
        }
    }

  return [NSArray arrayWithArray: arrayOut];
}

@end