summaryrefslogtreecommitdiff
path: root/csvncgi/ui-repolist.c
blob: 728f658d877d761854d881712e9aad73d3f10dbc (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
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
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#else
#include <stdint.h>
#endif
#include <stddef.h>   /* offsetof(3) */
#include <dirent.h>
#include <sys/stat.h> /* chmod(2)    */
#include <sys/file.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <limits.h>
#include <string.h>   /* strdup(3)   */
#include <libgen.h>   /* basename(3) */
#include <ctype.h>    /* tolower(3)  */
#include <errno.h>
#include <time.h>
#include <sys/time.h>
#include <pwd.h>
#include <grp.h>
#include <stdarg.h>
#include <locale.h>
#include <unistd.h>

#include <nls.h>

#include <defs.h>

#include <fatal.h>
#include <http.h>
#include <html.h>

#include <dlist.h>
#include <strbuf.h>
#include <repolist.h>
#include <wrapper.h>
#include <system.h>
#include <date.h>

#include <ctx.h>
#include <ui-shared.h>
#include <ui-repolist.h>



void csvn_print_direction( struct strbuf *sb, int prev, int next )
{
  strbuf_addf( sb, "              <div class=\"direction\">\n" );
  strbuf_addf( sb, "                <div class=\"row\">\n" );
  strbuf_addf( sb, "                  <div class=\"left col-prev\">\n" );
  strbuf_addf( sb, "                    <div class=\"prev-direction\">\n" );

  if( prev )
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d&%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, prev, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, prev );
  }
  else
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s\">&#x226a;&nbsp; Prev</a>\n", ctx.repo.relative_href );
  }

  strbuf_addf( sb, "                    </div>\n" );
  strbuf_addf( sb, "                  </div>\n" );
  strbuf_addf( sb, "                  <div class=\"right col-next\">\n" );
  strbuf_addf( sb, "                    <div class=\"next-direction\">\n" );

  if( next )
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d&%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, next, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s?ofs=%d\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, next );
  }
  else
  {
    if( ctx.env.query_string && *ctx.env.query_string )
      strbuf_addf( sb, "                      <a href=\"%s?%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href, ctx.env.query_string );
    else
      strbuf_addf( sb, "                      <a href=\"%s\">Next &nbsp;&#x226b;</a>\n", ctx.repo.relative_href );
  }

  strbuf_addf( sb, "                    </div>\n" );
  strbuf_addf( sb, "                  </div>\n" );
  strbuf_addf( sb, "                </div>\n" );
  strbuf_addf( sb, "              </div>\n" );
}

void csvn_print_repolist_header( struct strbuf *sb )
{
  strbuf_addf( sb, "              <div class=\"repo-list-header\">\n" );
  strbuf_addf( sb, "                <div class=\"row\">\n" );
  strbuf_addf( sb, "                  <div class=\"col-name\">Name</div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-desc\">Description</div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-owner\"><div class=\"repo-owner trunc\">Owner</div></div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-rev\"><div class=\"repo-rev trunc\">Rev</div></div>\n" );
  strbuf_addf( sb, "                  <div class=\"col-idle\"><div class=\"repo-idle trunc\">Idle</div></div>\n" );
  strbuf_addf( sb, "                </div>\n" );
  strbuf_addf( sb, "              </div>\n" );
}

void csvn_print_section_start( struct strbuf *sb, const char *name )
{
  if( !sb ) return;

  strbuf_addf( sb, "              <div class=\"section\">\n" );
  if( name && *name )
    strbuf_addf( sb, "                <div class=\"section-header\">%s</div>\n\n", name );
  else
    strbuf_addf( sb, "                <div class=\"section-header\"></div>\n\n" );
}

void csvn_print_section_stop( struct strbuf *sb )
{
  if( !sb ) return;
  strbuf_addf( sb, "              </div> <!-- end of section -->\n\n" );
}

void csvn_print_repo( struct strbuf *sb, struct repo *repo )
{
  struct variable repo_root   = { (unsigned char *)"repo-root",   { 0 }, DT_PATH   },
                  owner       = { (unsigned char *)"owner",       { 0 }, DT_STRING },
                  description = { (unsigned char *)"description", { 0 }, DT_STRING };
  struct variable *rroot = NULL;
  struct variable *auth = NULL;
  struct variable *desc = NULL;

  struct strbuf buf = STRBUF_INIT;
  size_t revision   = csvn_repo_last_changed_revision( &buf, repo );

  time_t time = 0;

  if( !sb || !repo || !repo->path ) return;

  rroot = lookup( repo, &repo_root );
  auth  = lookup( repo, &owner );
  desc  = lookup( repo, &description );
  time  = csvn_repo_last_changed_time( repo );

  strbuf_addf( sb, "                <div class=\"row\">\n" );
  if( rroot )
    strbuf_addf( sb, "                  <div class=\"col-name\"><a href=\"/%s/%s/\"><div class=\"repo-name\">%s</div></a></div>\n", (const char *)rroot->_v.vptr, repo->path, repo->path );
  else
    strbuf_addf( sb, "                  <div class=\"col-name\"><a href=\"/%s/\"><div class=\"repo-name\">%s</div></a></div>\n", repo->path, repo->path );

  if( desc )
    strbuf_addf( sb, "                  <div class=\"col-desc\"><div onclick=\"trunc(this)\" class=\"repo-desc trunc\">%s</div></div>\n", (const char *)desc->_v.vptr );
  else
    strbuf_addf( sb, "                  <div class=\"col-desc\"><div onclick=\"trunc(this)\" class=\"repo-desc trunc\">none</div></div>\n" );

  if( auth )
    strbuf_addf( sb, "                  <div class=\"col-owner\"><div onclick=\"trunc(this)\" class=\"repo-owner trunc\">%s</div></div>\n", (const char *)auth->_v.vptr );
  else
    strbuf_addf( sb, "                  <div class=\"col-owner\"><div onclick=\"trunc(this)\" class=\"repo-owner trunc\">none</div></div>\n" );

  if( revision )
    strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"repo-rev trunc\">%s</div></div>\n", buf.buf );
  else
    strbuf_addf( sb, "                  <div class=\"col-rev\"><div onclick=\"trunc(this)\" class=\"repo-rev trunc\">%s</div></div>\n", "0" );

  strbuf_release( &buf );

  if( time != -1 )
  {
    struct strbuf buf = STRBUF_INIT;
    csvn_print_age( &buf, time, 0, TM_YEAR );
    strbuf_addf( sb, "                  <div class=\"col-idle\"><div onclick=\"trunc(this)\" class=\"repo-idle trunc\">%s</div></div>\n", buf.buf );
    strbuf_release( &buf );
  }
  else
    strbuf_addf( sb, "                  <div class=\"col-idle\"><div onclick=\"trunc(this)\" class=\"repo-idle trunc\">%s</div></div>\n", "unknown" );

  strbuf_addf( sb, "                </div>\n\n" );
}


int csvn_print_page_repolist( struct strbuf *sb, struct dlist *config, int start_no, int n )
{
  struct dlist *list = NULL;
  int length = 0;

  if( !config || !sb ) return length;

  list = config;

  if( start_no > 0 )
  {
    list = parent_section_node_repolist_nth( config, start_no );
    {
      struct section *section = (struct section *)list->data;
      struct dlist *rlist = parent_rlist_node_repolist_nth( config, start_no );

      csvn_print_section_start( sb, (const char *)section->name );

      while( rlist )
      {
        struct repo *repo = (struct repo *)rlist->data;

        csvn_print_repo( sb, repo );
        if( n && ++length == n )
        {
          csvn_print_section_stop( sb );
          return length;
        }

        rlist = dlist_next( rlist );
      }
      csvn_print_section_stop( sb );
    }
    list = dlist_next( list );
  }

  while( list )
  {
    struct section *section = (struct section *)list->data;

    if( section->type == ST_REPOS )
    {
      csvn_print_section_start( sb, (const char *)section->name );

      struct dlist *rlist = section->list;
      while( rlist )
      {
        struct repo *repo = (struct repo *)rlist->data;

        csvn_print_repo( sb, repo );
        if( n && ++length == n )
        {
          csvn_print_section_stop( sb );
          return length;
        }

        rlist = dlist_next( rlist );
      }

      csvn_print_section_stop( sb );
    }
    list = dlist_next( list );
  }

  return length;
}


void csvn_print_repolist_page( void )
{
  FILE  *fp;
  struct strbuf buf = STRBUF_INIT;
  int psize = 200, rnum = 0, pos = 0, prev, next;

  fp = xfopen( ctx.page.header, "r" );
  (void)strbuf_env_fread( &buf, fp );
  fclose( fp );

  strbuf_addf( &buf, "        <div class=\"content segment\">\n" );
  strbuf_addf( &buf, "          <div class=\"container\">\n" );
  strbuf_addf( &buf, "            <div class=\"csvn-main-content\">\n" );

  if( ctx.vars.page_size && *ctx.vars.page_size )
    sscanf( ctx.vars.page_size, "%d", &psize );

  if( ctx.vars.num_of_repos && *ctx.vars.num_of_repos )
    sscanf( ctx.vars.num_of_repos, "%d", &rnum );

  if( ctx.query.ofs > 0)       pos = ctx.query.ofs;
  if( ctx.query.ofs > rnum-1 ) pos = rnum - rnum % psize;
  if( pos < 0 )                pos = 0;

  prev = pos - psize;
  if( prev < 0 ) prev = 0;

  next = pos + psize;
  if( next > rnum-1 ) next = rnum - rnum % psize;
  if( next < 0 ) next = 0;

  csvn_print_repolist_header( &buf );
  (void)csvn_print_page_repolist( &buf, config, pos, psize );
  csvn_print_direction( &buf, prev, next );

  strbuf_addf( &buf, "            </div> <!-- End of csvn-main-content -->\n" );
  strbuf_addf( &buf, "          </div> <!-- End of container -->\n" );
  strbuf_addf( &buf, "        </div> <!-- End of content segment -->\n" );

  fp = xfopen( ctx.page.footer, "r" );
  (void)strbuf_env_fread( &buf, fp );
  fclose( fp );

  ctx.page.size = buf.len;
  csvn_print_http_headers();
  strbuf_write( &buf, STDOUT_FILENO );
  strbuf_release( &buf );
}