a b/src/qtgui/rtitool.cpp
1
/* Copyright (C) 2005 J.F.Dockes
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
#include "autoconfig.h"
18
19
#include <stdio.h>
20
#include <unistd.h>
21
#include <fcntl.h>
22
#include <signal.h>
23
24
#include <string>
25
using std::string;
26
27
#include <QCheckBox>
28
#include <QMessageBox>
29
30
#include "recoll.h"
31
#include "rtitool.h"
32
#include "smallut.h"
33
#include "pathut.h"
34
#include "copyfile.h"
35
#include "readfile.h"
36
#include "execmd.h"
37
38
static const char *rautostartfile = ".config/autostart/recollindex.desktop";
39
40
// Just in case we don't find the file in the shared dir, have a
41
// default text ready
42
static const char *desktopfiletext = 
43
    "[Desktop Entry]\n"
44
    "Name=Recoll real time indexer\n"
45
    "Comment=Runs in background to extract and index text from modified "
46
     "documents\n"
47
    "Icon=system-run\n"
48
    "Exec=recollindex -w 60 -m\n"
49
    "Terminal=false\n"
50
    "TerminalOptions=\n"
51
    "Type=Application\n"
52
    "Categories=Utility;Filesystem;Database;\n"
53
    "NoDisplay=true\n"
54
    "X-GNOME-Autostart-enabled=true\n"
55
    "X-KDE-autostart-after=panel\n"
56
    "X-KDE-UniqueApplet=true\n"
57
    ;
58
59
void RTIToolW::init()
60
{
61
    connect(this->sesCB, SIGNAL(clicked(bool)), 
62
      this, SLOT(sesclicked(bool)));
63
    string autostartfile = path_cat(path_home(), rautostartfile);
64
    if (access(autostartfile.c_str(), 0) == 0) {
65
  sesCB->setChecked(true);
66
    }
67
}
68
69
void RTIToolW::sesclicked(bool on)
70
{
71
    nowCB->setEnabled(on);
72
    if (!on)
73
  nowCB->setChecked(false);
74
}
75
76
void RTIToolW::accept()
77
{
78
    bool exitdial = false;
79
    string autostartfile = path_cat(path_home(), rautostartfile);
80
81
    if (sesCB->isChecked()) {
82
  // Setting up daemon indexing autostart
83
84
  if (::access(autostartfile.c_str(), 0) == 0) {
85
      QString msg = tr("Replacing: ") + 
86
      QString::fromLocal8Bit(autostartfile.c_str());
87
  
88
      QMessageBox::Button rep = 
89
      QMessageBox::question(this, tr("Replacing file"), msg,
90
                    QMessageBox::Ok | QMessageBox::Cancel);
91
      if (rep != QMessageBox::Ok) {
92
      goto out;
93
      }
94
  }
95
96
  string text;
97
  if (theconfig) {
98
      string sourcefile = path_cat(theconfig->getDatadir(), "examples");
99
      sourcefile = path_cat(sourcefile, "recollindex.desktop");
100
      if (::access(sourcefile.c_str(), 0) == 0) {
101
      file_to_string(sourcefile, text);
102
      }
103
  }
104
  if (text.empty())
105
      text = desktopfiletext;
106
107
  int fd = ::open(autostartfile.c_str(), O_WRONLY|O_CREAT, 0644);
108
  if (fd < 0 || ::write(fd, text.c_str(), size_t(text.size())) 
109
      != ssize_t(text.size()) || ::close(fd) != 0) {
110
      if (fd >=0)
111
      ::close(fd);
112
      QString msg = tr("Can't create: ") + 
113
      QString::fromLocal8Bit(autostartfile.c_str());
114
      QMessageBox::warning(0, tr("Warning"), msg, QMessageBox::Ok);
115
      return;
116
  }
117
  ::close(fd);
118
119
  if (nowCB->isChecked()) {
120
      ExecCmd cmd;
121
      list<string> args; 
122
      int status;
123
124
      args.push_back("-m");
125
      args.push_back("-w");
126
      args.push_back("0");
127
      status = cmd.doexec("recollindex", args, 0, 0);
128
      if (status) {
129
      QMessageBox::warning(0, tr("Warning"), 
130
                   tr("Could not execute recollindex"), 
131
                   QMessageBox::Ok);
132
      goto out;
133
      }
134
  }
135
136
  exitdial = true;
137
    } else {
138
  // Turning autostart off
139
  if (::access(autostartfile.c_str(), 0) == 0) {
140
      QString msg = tr("Deleting: ") + 
141
      QString::fromLocal8Bit(autostartfile.c_str());
142
  
143
      QMessageBox::Button rep = 
144
      QMessageBox::question(this, tr("Deleting file"), msg,
145
                    QMessageBox::Ok | QMessageBox::Cancel);
146
      if (rep == QMessageBox::Ok) {
147
      exitdial = true;
148
      unlink(autostartfile.c_str());
149
      if (theconfig) {
150
          Pidfile pidfile(theconfig->getPidfile());
151
          pid_t pid;
152
          if ((pid = pidfile.open()) != 0) {
153
          QMessageBox::Button rep = 
154
              QMessageBox::question(this, 
155
                       tr("Removing autostart"), 
156
             tr("Autostart file deleted. Kill current process too ?"),
157
                    QMessageBox::Yes | QMessageBox::No);
158
          if (rep == QMessageBox::Yes) {
159
              kill(pid, SIGTERM);
160
          }
161
          }
162
      }
163
      }
164
  } else {
165
      exitdial = true;
166
  }
167
    }
168
169
out:
170
    if (exitdial)
171
  QDialog::accept();
172
}