summaryrefslogtreecommitdiff
path: root/src/delete.c
blob: ce62951faf08b33ab39f491d8f9b27a2df92eb8f (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
/* ncdu - NCurses Disk Usage 
  
  Copyright (c) 2007 Yoran Heling

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:
  
  The above copyright notice and this permission notice shall be included
  in all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/

#include "ncdu.h"

suseconds_t lastupdate;


void drawConfirm(struct dir *del, int sel) {
  WINDOW *cfm;

  cfm = newwin(6, 60, winrows/2-3, wincols/2-30);
  box(cfm, 0, 0);
  wattron(cfm, A_BOLD);
  mvwaddstr(cfm, 0, 4, "Confirm delete");
  wattroff(cfm, A_BOLD);

  mvwprintw(cfm, 1, 2, "Are you sure you want to delete \"%s\"%c",
    cropdir(del->name, 21), del->flags & FF_DIR ? ' ' : '?');
  if(del->flags & FF_DIR) 
    mvwprintw(cfm, 2, 18, "and all of its contents?");

  if(sel == 0)
    wattron(cfm, A_REVERSE);
  mvwaddstr(cfm, 4, 15, "yes");
  wattroff(cfm, A_REVERSE);
  if(sel == 1)
    wattron(cfm, A_REVERSE);
  mvwaddstr(cfm, 4, 24, "no");
  wattroff(cfm, A_REVERSE);
  if(sel == 2)
    wattron(cfm, A_REVERSE);
  mvwaddstr(cfm, 4, 31, "don't ask me again");
  wattroff(cfm, A_REVERSE);

  wrefresh(cfm);
  delwin(cfm);
}


/* show progress */
void drawProgress(char *file) {
  WINDOW *prg;

  prg = newwin(6, 60, winrows/2-3, wincols/2-30);
  nodelay(prg, 1);
  box(prg, 0, 0);
  wattron(prg, A_BOLD);
  mvwaddstr(prg, 0, 4, "Deleting...");
  wattroff(prg, A_BOLD); 

  mvwaddstr(prg, 1, 2, cropdir(file, 47));
  mvwaddstr(prg, 5, 41, "Press q to abort");

  wrefresh(prg);
  delwin(prg);
}


/* show error dialog */
void drawError(int sel, char *file) {
  WINDOW *err;

  err = newwin(6, 60, winrows/2-3, wincols/2-30);
  box(err, 0, 0);
  wattron(err, A_BOLD);
  mvwaddstr(err, 0, 4, "Error!");
  wattroff(err, A_BOLD);

  mvwprintw(err, 1, 2, "Can't delete %s:", cropdir(file, 42));
  mvwaddstr(err, 2, 4, strerror(errno));

  if(sel == 0)
    wattron(err, A_REVERSE);
  mvwaddstr(err, 4, 14, "abort");
  wattroff(err, A_REVERSE);
  if(sel == 1)
    wattron(err, A_REVERSE);
  mvwaddstr(err, 4, 23, "ignore");
  wattroff(err, A_REVERSE);
  if(sel == 2)
    wattron(err, A_REVERSE);
  mvwaddstr(err, 4, 33, "ignore all");
  wattroff(err, A_REVERSE);

  wrefresh(err);
  delwin(err); 
}


struct dir *deleteDir(struct dir *dr) {
  struct dir *nxt, *cur;
  int ch, sel = 0;
  char file[PATH_MAX];
  struct timeval tv;

  getpath(dr, file);
  strcat(file, "/");
  strcat(file, dr->name);

 /* check for input or screen resizes */
  nodelay(stdscr, 1);
  while((ch = getch()) != ERR) {
    if(ch == 'q')
      return(NULL);
    if(ch == KEY_RESIZE) {
      ncresize();
      drawBrowser(0);
      drawProgress(file);
    }
  }
  nodelay(stdscr, 0);

 /* don't update the screen with shorter intervals than sdelay */
  gettimeofday(&tv, (void *)NULL);
  tv.tv_usec = (1000*(tv.tv_sec % 1000) + (tv.tv_usec / 1000)) / sdelay;
  if(lastupdate != tv.tv_usec) {
    drawProgress(file);
    lastupdate = tv.tv_usec;
  }

 /* do the actual deleting */
  if(dr->flags & FF_DIR) {
    if(dr->sub != NULL) {
      nxt = dr->sub;
      while(nxt->prev != NULL)
        nxt = nxt->prev;
      while(nxt != NULL) {
        cur = nxt;
        nxt = cur->next;
        if(cur->flags & FF_PAR) {
          freedir(cur);
          continue;
        }
        if(deleteDir(cur) == NULL)
          return(NULL);
      }
    }
    ch = rmdir(file);
  } else
    ch = unlink(file); 

 /* error occured, ask user what to do */
  if(ch == -1 && !(sflags & SF_IGNE)) {
    drawError(sel, file);
    while((ch = getch())) {
      switch(ch) {
        case KEY_LEFT:
          if(--sel < 0)
            sel = 0;
          break;
        case KEY_RIGHT:
          if(++sel > 2)
            sel = 2;
          break;
        case 10:
          if(sel == 0)
            return(NULL);
          if(sel == 2)
            sflags |= SF_IGNE;
          goto ignore;
        case 'q':
          return(NULL);
        case KEY_RESIZE:
          ncresize();
          drawBrowser(0);
          break;
      }
      drawError(sel, file);
    }
  };
  ignore: 

  return(freedir(dr));
}


struct dir *showDelete(struct dir *dr) {
  int ch, sel = 1;
  struct dir *ret;

 /* confirm */
  if(sflags & SF_NOCFM)
    goto doit;
  
  drawConfirm(dr, sel);
  while((ch = getch())) {
    switch(ch) {
      case KEY_LEFT:
        if(--sel < 0)
          sel = 0;
        break;
      case KEY_RIGHT:
        if(++sel > 2)
          sel = 2;
        break;
      case 10:
        if(sel == 1)
          return(dr);
        if(sel == 2)
          sflags |= SF_NOCFM;
        goto doit;
      case 'q':
        return(dr);
      case KEY_RESIZE:
        ncresize();
        drawBrowser(0);
        break;
    }
    drawConfirm(dr, sel);
  }

  doit:
  lastupdate = 999; /* just some random high value as initialisation */

  ret = deleteDir(dr);

  return(ret == NULL ? dr : ret);
}