~ubuntu-branches/ubuntu/quantal/recoll/quantal

« back to all changes in this revision

Viewing changes to qtgui/rtitool.cpp

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry
  • Date: 2012-03-27 12:15:51 UTC
  • mfrom: (1.3.8)
  • Revision ID: package-import@ubuntu.com-20120327121551-nmntidzpehudushy
Tags: 1.17.1-1
* New upstream release.
* Enable Python module resulting into new binary: python-recoll.
* debian/control:
  + Updated Build-Deps: libqtwebkit-dev, python-all-dev.
  + Added python-recoll binary.
  + Updated Standards-Version to 3.9.3
* debian/rules:
  + Build Python module by default.
* debian/recoll.menu, debian/python-recoll.install, debian/recoll.install:
  + Changes for new binary package.
* debian/copyright:
  + Updated to copyright-format 1.0
  + Updated upstream and Debian copyright.
  + Fixed unicode.org/copyright.html URL.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include <sys/stat.h>
 
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
        // Try to create .config and autostart anyway. If they exists this will 
 
108
        // do nothing. An error will be detected when we try to create the file
 
109
        string dir = path_cat(path_home(), ".config");
 
110
        mkdir(dir.c_str(), 0700);
 
111
        dir = path_cat(dir, "autostart");
 
112
        mkdir(dir.c_str(), 0700);
 
113
 
 
114
        int fd = ::open(autostartfile.c_str(), O_WRONLY|O_CREAT, 0644);
 
115
        if (fd < 0 || ::write(fd, text.c_str(), size_t(text.size())) 
 
116
            != ssize_t(text.size()) || ::close(fd) != 0) {
 
117
            if (fd >=0)
 
118
                ::close(fd);
 
119
            QString msg = tr("Can't create: ") + 
 
120
                QString::fromLocal8Bit(autostartfile.c_str());
 
121
            QMessageBox::warning(0, tr("Warning"), msg, QMessageBox::Ok);
 
122
            return;
 
123
        }
 
124
        ::close(fd);
 
125
 
 
126
        if (nowCB->isChecked()) {
 
127
            ExecCmd cmd;
 
128
            list<string> args; 
 
129
            int status;
 
130
 
 
131
            args.push_back("-m");
 
132
            args.push_back("-w");
 
133
            args.push_back("0");
 
134
            status = cmd.doexec("recollindex", args, 0, 0);
 
135
            if (status) {
 
136
                QMessageBox::warning(0, tr("Warning"), 
 
137
                                     tr("Could not execute recollindex"), 
 
138
                                     QMessageBox::Ok);
 
139
                goto out;
 
140
            }
 
141
        }
 
142
 
 
143
        exitdial = true;
 
144
    } else {
 
145
        // Turning autostart off
 
146
        if (::access(autostartfile.c_str(), 0) == 0) {
 
147
            QString msg = tr("Deleting: ") + 
 
148
                QString::fromLocal8Bit(autostartfile.c_str());
 
149
        
 
150
            QMessageBox::Button rep = 
 
151
                QMessageBox::question(this, tr("Deleting file"), msg,
 
152
                                      QMessageBox::Ok | QMessageBox::Cancel);
 
153
            if (rep == QMessageBox::Ok) {
 
154
                exitdial = true;
 
155
                unlink(autostartfile.c_str());
 
156
                if (theconfig) {
 
157
                    Pidfile pidfile(theconfig->getPidfile());
 
158
                    pid_t pid;
 
159
                    if ((pid = pidfile.open()) != 0) {
 
160
                        QMessageBox::Button rep = 
 
161
                            QMessageBox::question(this, 
 
162
                             tr("Removing autostart"), 
 
163
                       tr("Autostart file deleted. Kill current process too ?"),
 
164
                                          QMessageBox::Yes | QMessageBox::No);
 
165
                        if (rep == QMessageBox::Yes) {
 
166
                            kill(pid, SIGTERM);
 
167
                        }
 
168
                    }
 
169
                }
 
170
            }
 
171
        } else {
 
172
            exitdial = true;
 
173
        }
 
174
    }
 
175
 
 
176
out:
 
177
    if (exitdial)
 
178
        QDialog::accept();
 
179
}