summaryrefslogtreecommitdiff
path: root/src/filerenamer.cpp
blob: c6172dc6aaa9e0896186721182b4906127c5b7bc (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
#include "filerenamer.h"
#include <QFileInfo>
#include <QMessageBox>
#include <log.h>
#include <utility.h>

using namespace MOBase;

FileRenamer::FileRenamer(QWidget* parent, QFlags<RenameFlags> flags)
    : m_parent(parent), m_flags(flags)
{
  // sanity check for flags
  if ((m_flags & (HIDE | UNHIDE)) == 0) {
    log::error("renameFile() missing hide flag");
    // doesn't really matter, it's just for text
    m_flags = HIDE;
  }
}

FileRenamer::RenameResults FileRenamer::rename(const QString& oldName,
                                               const QString& newName)
{
  log::debug("renaming {} to {}", oldName, newName);

  if (QFileInfo(newName).exists()) {
    log::debug("{} already exists", newName);

    // target file already exists, confirm replacement
    auto answer = confirmReplace(newName);

    switch (answer) {
    case DECISION_SKIP: {
      // user wants to skip this file
      log::debug("skipping {}", oldName);
      return RESULT_SKIP;
    }

    case DECISION_REPLACE: {
      log::debug("removing {}", newName);

      // user wants to replace the file, so remove it
      const auto r = shell::Delete(QFileInfo(newName));

      if (!r.success()) {
        log::error("failed to remove '{}': {}", newName, r.toString());

        // removal failed, warn the user and allow canceling
        if (!removeFailed(newName, r)) {
          log::debug("canceling {}", oldName);
          // user wants to cancel
          return RESULT_CANCEL;
        }

        // ignore this file and continue on
        log::debug("skipping {}", oldName);
        return RESULT_SKIP;
      }

      break;
    }

    case DECISION_CANCEL:  // fall-through
    default: {
      // user wants to stop
      log::debug("canceling");
      return RESULT_CANCEL;
    }
    }
  }

  // target either didn't exist or was removed correctly
  const auto r = shell::Rename(QFileInfo(oldName), QFileInfo(newName));

  if (!r.success()) {
    log::error("failed to rename '{}' to '{}': {}", oldName, newName, r.toString());

    // renaming failed, warn the user and allow canceling
    if (!renameFailed(oldName, newName, r)) {
      // user wants to cancel
      log::debug("canceling");
      return RESULT_CANCEL;
    }

    // ignore this file and continue on
    log::debug("skipping {}", oldName);
    return RESULT_SKIP;
  }

  // everything worked
  log::debug("successfully renamed {} to {}", oldName, newName);
  return RESULT_OK;
}

FileRenamer::RenameDecision FileRenamer::confirmReplace(const QString& newName)
{
  if (m_flags & REPLACE_ALL) {
    // user wants to silently replace all
    log::debug("user has selected replace all");
    return DECISION_REPLACE;
  } else if (m_flags & REPLACE_NONE) {
    // user wants to silently skip all
    log::debug("user has selected replace none");
    return DECISION_SKIP;
  }

  QString text;

  if (m_flags & HIDE) {
    text =
        QObject::tr("The hidden file \"%1\" already exists. Replace it?").arg(newName);
  } else if (m_flags & UNHIDE) {
    text =
        QObject::tr("The visible file \"%1\" already exists. Replace it?").arg(newName);
  }

  auto buttons = QMessageBox::Yes | QMessageBox::No;
  if (m_flags & MULTIPLE) {
    // only show these buttons when there are multiple files to replace
    buttons |= QMessageBox::YesToAll | QMessageBox::NoToAll | QMessageBox::Cancel;
  }

  const auto answer =
      QMessageBox::question(m_parent, QObject::tr("Replace file?"), text, buttons);

  switch (answer) {
  case QMessageBox::Yes:
    log::debug("user wants to replace");
    return DECISION_REPLACE;

  case QMessageBox::No:
    log::debug("user wants to skip");
    return DECISION_SKIP;

  case QMessageBox::YesToAll:
    log::debug("user wants to replace all");
    // remember the answer
    m_flags |= REPLACE_ALL;
    return DECISION_REPLACE;

  case QMessageBox::NoToAll:
    log::debug("user wants to replace none");
    // remember the answer
    m_flags |= REPLACE_NONE;
    return DECISION_SKIP;

  case QMessageBox::Cancel:  // fall-through
  default:
    log::debug("user wants to cancel");
    return DECISION_CANCEL;
  }
}

bool FileRenamer::removeFailed(const QString& name, const shell::Result& r)
{
  QMessageBox::StandardButtons buttons = QMessageBox::Ok;
  if (m_flags & MULTIPLE) {
    // only show cancel for multiple files
    buttons |= QMessageBox::Cancel;
  }

  const auto answer = QMessageBox::critical(
      m_parent, QObject::tr("File operation failed"),
      QObject::tr("Failed to remove \"%1\": %2").arg(name).arg(r.toString()), buttons);

  if (answer == QMessageBox::Cancel) {
    // user wants to stop
    log::debug("user wants to cancel");
    return false;
  }

  // skip this one and continue
  log::debug("user wants to skip");
  return true;
}

bool FileRenamer::renameFailed(const QString& oldName, const QString& newName,
                               const shell::Result& r)
{
  QMessageBox::StandardButtons buttons = QMessageBox::Ok;
  if (m_flags & MULTIPLE) {
    // only show cancel for multiple files
    buttons |= QMessageBox::Cancel;
  }

  const auto answer =
      QMessageBox::critical(m_parent, QObject::tr("File operation failed"),
                            QObject::tr("Failed to rename file: %1.\r\n\r\n"
                                        "Source:\r\n\"%2\"\r\n\r\n"
                                        "Destination:\r\n\"%3\"")
                                .arg(r.toString())
                                .arg(QDir::toNativeSeparators(oldName))
                                .arg(QDir::toNativeSeparators(newName)),
                            buttons);

  if (answer == QMessageBox::Cancel) {
    // user wants to stop
    log::debug("user wants to cancel");
    return false;
  }

  // skip this one and continue
  log::debug("user wants to skip");
  return true;
}