File: selection.c

package info (click to toggle)
tina 0.1.12-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 500 kB
  • sloc: ansic: 1,571; sh: 1,344; makefile: 22
file content (126 lines) | stat: -rw-r--r-- 3,143 bytes parent folder | download | duplicates (3)
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
/*
 * tina - a personal information manager
 * Copyright (C) 2001  Matt Kraai
 * Copyright (C) 2016  Peter Pentchev <roam@ringlet.net>
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include <stdlib.h>
#include <string.h>

#include "memory.h"
#include "selection.h"

struct selection *
selection_new_with_database (struct database *db)
{
  struct selection *s;

  s = xcalloc (1, sizeof (struct selection));
  s->db = db;
  selection_refresh (s);

  return s;
}

void
selection_delete (struct selection *s)
{
  free (s->category);
  free (s->items);
  free (s);
}

void
selection_refresh (struct selection *s)
{
  size_t i;

  free (s->items);
  s->items = NULL;
  s->nitems = 0;

  for (i = 0; i < s->db->nitems; i++)
    if ((s->category == NULL
	 && s->db->items[i]->ncategories == 0)
	|| (s->category != NULL
	    && (item_category_member_p (s->db->items[i], s->category)
		|| (s->category[0] == '!'
		    && ! item_category_member_p (s->db->items[i],
						 s->category + 1)))))
      {
	s->items = xrealloc (s->items,
			     sizeof (struct item *) * (s->nitems + 1));
	s->items[s->nitems++] = s->db->items[i];
      }
}

void
selection_category_set (struct selection *s, const char *category)
{
  free (s->category);
  s->category = category != NULL ? xstrdup (category) : NULL;

  selection_refresh (s);
}

int
selection_item_index (struct selection *s, struct item *it)
{
  size_t pos;

  for (pos = 0; pos < s->nitems; pos++)
    if (s->items[pos] == it)
      break;

  return pos;
}

void
selection_item_add (struct selection *s, struct item *it, size_t pos)
{
  if (s->nitems < pos)
    abort ();

  s->items = xrealloc (s->items, sizeof (struct item *) * (s->nitems + 1));
  memmove (s->items + pos + 1, s->items + pos,
	   sizeof (struct item *) * (s->nitems - pos));
  s->items[pos] = it;
  s->nitems++;

  if (s->category != NULL)
    {
      if (s->category[0] == '!' && item_category_member_p (it, s->category + 1))
	item_category_remove (it, s->category + 1);
      else if (! item_category_member_p (it, s->category))
	item_category_add (it, s->category);
    }
}

void
selection_item_remove (struct selection *s, size_t pos)
{
  if (s->nitems - 1 < pos)
    abort ();

  if (s->category != NULL)
    item_category_remove (s->items[pos], s->category);

  memmove (s->items + pos, s->items + pos + 1,
	   sizeof (struct item *) * (s->nitems - pos - 1));
  s->items = xrealloc (s->items, sizeof (struct item *) * (s->nitems - 1));
  s->nitems--;
}