Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/woip/app/SearchView.m
blob: 993613610a2aab791dbaa4c601e212a6fb06e4e4 (plain)
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
#import "SearchView.h"
#import "WikiApp.h"

#define MAXRES 10

int nresults = 0;
uint32_t blocks[MAXRES];
char *matches[MAXRES];
char *buf;

bool chandleResult(char *s) {
  NSLog(@"chandleResult: %s", s);
  buf = xalloc(MAXSTR * sizeof(char));
  strcpy(buf, s);

  NSLog(@"buf: 0x%x, %s", buf, buf);

  char *blockpos = strrchr(buf, ' ');
  int c;

  if(nresults >= MAXRES) return false;

  if(blockpos) {
    *(blockpos - 1) = '\0'; 
    blockpos++;

    blocks[nresults] = atoi(blockpos);

    matches[nresults] = buf;
  } else
    fatal("invalid: '%s'", buf);

  NSLog(@"chandleResult returning...");
  return ++nresults < MAXRES;
}

void zeroResults() { 
  int i;
  for(i = 0; i < nresults; i++) free(matches[i]);
  nresults = 0;
}
int numResults() { return nresults; }
char *resultAt(int i) { return matches[i]; }
uint32_t blockAt(int i) { return blocks[i]; }

@implementation SearchView

-(void) initMatches {
  int i;
  for(i = 0; i < MAXRES; i++)
    matches[i] = xalloc(MAXSTR * sizeof(char));
  buf = NULL;
}

-(id) initWithFrame: (struct CGRect) frame
{
  self = [super initWithFrame: frame];  

  curNeedle = xalloc(MAXSTR * sizeof(char));
  [self initMatches];
  sthread = nil;
  lock = [[NSRecursiveLock alloc] init];

  struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
  //TODO: re-enable me when I want progress bar
  //table = [[UICompletionTable alloc] initWithFrame: CGRectMake(0, 0, frame.size.width, frame.size.height - 240)];
  table = [[UICompletionTable alloc] initWithFrame: CGRectMake(0, 0, frame.size.width, frame.size.height - 215)];

  [table setDelegate: self];

  [self addSubview: table];

  prog = [[UIProgressBar alloc] initWithFrame: CGRectMake(0, 180, 320, 20)];
  [prog setStyle: 0];
  [prog setProgress: 1.0];
  //[self addSubview: prog];
  //TODO: re-enable above to get progress bar

  kbrd = [[UIKeyboard alloc] initWithFrame: CGRectMake(0, 200, 320, 480)];
	[self addSubview: kbrd];

  dump = xalloc(sizeof(wp_dump));

  load_dump(dump,
      "/var/root/wp/processed",
      "/var/root/wp/locate.db",
      "/var/root/wp/locate.prefixdb",
      "/var/root/wp/blocks.db");

  updater = [NSTimer scheduledTimerWithTimeInterval: 1 
           target: self
           selector: @selector(refreshResults:)
           userInfo: nil
           repeats: YES]; 

  return self; 
}

-(void) stopSearch {
  NSLog(@"stopSearch");
  kill_search();
  while(sthread && [sthread isExecuting]) {
    //usleep(100000);
    //NSLog(@"waiting for sthread");
  }
}


-(void) runSearch {
  NSLog(@"runSearch with needle: '%s'", curNeedle);
  NSAutoreleasePool* pool = [NSAutoreleasePool new];
  search(&dump->index, curNeedle, &chandleResult, NULL, true, true); 
  needRefresh = true;
  NSLog(@"search done");
  [pool release];
}

-(void) refreshResults: (NSTimer *) timer {
  NSLog(@"refreshResults");
  [lock lock];
  if(needRefresh || (sthread && [sthread isExecuting])) {
    [table reloadData];
    if(needRefresh) needRefresh = false;
  }
  double d;
  search_progress(&dump->index, &d);
  [prog setProgress: d];
  [prog updateIfNecessary];
  [lock unlock];
}

-(void) updateResults {
  NSLog(@"Updating results for '%@'", needle);
  [self stopSearch];
  zeroResults();
  curResults = 0;
  needRefresh = false;

  /* TODO: keep the table results that match the new needle */
  [table reloadData];
  [prog setProgress: 0.0];

  strncpy(curNeedle, [[needle capitalizedString] UTF8String], MAXSTR);

  sthread = [[NSThread alloc] initWithTarget: self selector: @selector(runSearch) object: nil];
  [sthread start];
}

-(int) numberOfCompletionsInTable:(UICompletionTable *) t {
  return numResults();
}

-(UITableCell *) table:(UITable *)t completionAtIndex: (int) index {
  return [NSString stringWithUTF8String: resultAt(index)];
}

-(void) table: (UICompletionTable *) t didSelectCompletionAtIndex: (int) i {
  [aview loadArticle: [NSString stringWithUTF8String: resultAt(i)] fromBlock: blockAt(i)];
  [parent switchToArticle];
}

- (void) setParent: (WikiApp *) w {
  parent = w;
}

-(void) setNeedle: (NSString *) s {
  [lock lock];
  needle = s;

  if([s length] > 0 && [s characterAtIndex: 0] != ' ')
    [self updateResults];
  [lock unlock];
}

- (void) setArticleView: (ArticleView *) a {
  aview = a;
  [aview setDump: dump];
}
@end