Parent: [8ff5d6] (diff)

Child: [78bd8d] (diff)

Download this file

rtitool.cpp    180 lines (160 with data), 5.0 kB

  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
/* Copyright (C) 2005 J.F.Dockes
* 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 "autoconfig.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/stat.h>
#include <string>
using std::string;
#include <QCheckBox>
#include <QMessageBox>
#include "recoll.h"
#include "rtitool.h"
#include "smallut.h"
#include "pathut.h"
#include "copyfile.h"
#include "readfile.h"
#include "execmd.h"
static const char *rautostartfile = ".config/autostart/recollindex.desktop";
// Just in case we don't find the file in the shared dir, have a
// default text ready
static const char *desktopfiletext =
"[Desktop Entry]\n"
"Name=Recoll real time indexer\n"
"Comment=Runs in background to extract and index text from modified "
"documents\n"
"Icon=system-run\n"
"Exec=recollindex -w 60 -m\n"
"Terminal=false\n"
"TerminalOptions=\n"
"Type=Application\n"
"Categories=Utility;Filesystem;Database;\n"
"NoDisplay=true\n"
"X-GNOME-Autostart-enabled=true\n"
"X-KDE-autostart-after=panel\n"
"X-KDE-UniqueApplet=true\n"
;
void RTIToolW::init()
{
connect(this->sesCB, SIGNAL(clicked(bool)),
this, SLOT(sesclicked(bool)));
string autostartfile = path_cat(path_home(), rautostartfile);
if (access(autostartfile.c_str(), 0) == 0) {
sesCB->setChecked(true);
}
}
void RTIToolW::sesclicked(bool on)
{
nowCB->setEnabled(on);
if (!on)
nowCB->setChecked(false);
}
void RTIToolW::accept()
{
bool exitdial = false;
string autostartfile = path_cat(path_home(), rautostartfile);
if (sesCB->isChecked()) {
// Setting up daemon indexing autostart
if (::access(autostartfile.c_str(), 0) == 0) {
QString msg = tr("Replacing: ") +
QString::fromLocal8Bit(autostartfile.c_str());
QMessageBox::Button rep =
QMessageBox::question(this, tr("Replacing file"), msg,
QMessageBox::Ok | QMessageBox::Cancel);
if (rep != QMessageBox::Ok) {
goto out;
}
}
string text;
if (theconfig) {
string sourcefile = path_cat(theconfig->getDatadir(), "examples");
sourcefile = path_cat(sourcefile, "recollindex.desktop");
if (::access(sourcefile.c_str(), 0) == 0) {
file_to_string(sourcefile, text);
}
}
if (text.empty())
text = desktopfiletext;
// Try to create .config and autostart anyway. If they exists this will
// do nothing. An error will be detected when we try to create the file
string dir = path_cat(path_home(), ".config");
mkdir(dir.c_str(), 0700);
dir = path_cat(dir, "autostart");
mkdir(dir.c_str(), 0700);
int fd = ::open(autostartfile.c_str(), O_WRONLY|O_CREAT, 0644);
if (fd < 0 || ::write(fd, text.c_str(), size_t(text.size()))
!= ssize_t(text.size()) || ::close(fd) != 0) {
if (fd >=0)
::close(fd);
QString msg = tr("Can't create: ") +
QString::fromLocal8Bit(autostartfile.c_str());
QMessageBox::warning(0, tr("Warning"), msg, QMessageBox::Ok);
return;
}
::close(fd);
if (nowCB->isChecked()) {
ExecCmd cmd;
list<string> args;
int status;
args.push_back("-m");
args.push_back("-w");
args.push_back("0");
status = cmd.doexec("recollindex", args, 0, 0);
if (status) {
QMessageBox::warning(0, tr("Warning"),
tr("Could not execute recollindex"),
QMessageBox::Ok);
goto out;
}
}
exitdial = true;
} else {
// Turning autostart off
if (::access(autostartfile.c_str(), 0) == 0) {
QString msg = tr("Deleting: ") +
QString::fromLocal8Bit(autostartfile.c_str());
QMessageBox::Button rep =
QMessageBox::question(this, tr("Deleting file"), msg,
QMessageBox::Ok | QMessageBox::Cancel);
if (rep == QMessageBox::Ok) {
exitdial = true;
unlink(autostartfile.c_str());
if (theconfig) {
Pidfile pidfile(theconfig->getPidfile());
pid_t pid;
if ((pid = pidfile.open()) != 0) {
QMessageBox::Button rep =
QMessageBox::question(this,
tr("Removing autostart"),
tr("Autostart file deleted. Kill current process too ?"),
QMessageBox::Yes | QMessageBox::No);
if (rep == QMessageBox::Yes) {
kill(pid, SIGTERM);
}
}
}
}
} else {
exitdial = true;
}
}
out:
if (exitdial)
QDialog::accept();
}