Parent: [4d35cb] (diff)

Child: [c6e228] (diff)

Download this file

rclm_saveload.cpp    137 lines (119 with data), 4.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
/* 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"
/** Saving and restoring named queries */
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <QSettings>
#include <QMessageBox>
#include <QFileDialog>
#include "rclmain_w.h"
#include "debuglog.h"
#include "readfile.h"
#include "xmltosd.h"
#include "searchdata.h"
#include "copyfile.h"
using namespace std;
using namespace Rcl;
static QString prevDir()
{
QSettings settings;
QString prevdir =
settings.value("/Recoll/prefs/lastQuerySaveDir").toString();
string defpath = path_cat(theconfig->getConfDir(), "saved_queries");
if (prevdir.isEmpty()) {
if (access(defpath.c_str(), 0) != 0) {
mkdir(defpath.c_str(), 0700);
}
return QString::fromLocal8Bit(defpath.c_str());
} else {
return prevdir;
}
}
void RclMain::saveLastQuery()
{
string xml;
if (lastSearchSimple()) {
xml = sSearch->asXML();
} else {
if (g_advshistory) {
STD_SHARED_PTR<Rcl::SearchData> sd;
sd = g_advshistory->getnewest();
if (sd) {
xml = sd->asXML();
}
}
}
if (xml.empty()) {
QMessageBox::information(this, tr("No search"),
tr("No preserved previous search"));
return;
}
xml = string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") +
"<recollquery version='1.0'>\n" + xml + "\n</recollquery>\n";
QFileDialog fileDialog(this, tr("Choose file to save"));
fileDialog.setNameFilter(tr("Saved Queries (*.rclq)"));
fileDialog.setDefaultSuffix("rclq");
fileDialog.setAcceptMode(QFileDialog::AcceptSave);
fileDialog.setDirectory(prevDir());
if (!fileDialog.exec())
return;
QString s = fileDialog.selectedFiles().first();
if (s.isEmpty()) {
return;
}
string tofile((const char *)s.toLocal8Bit());
LOGDEB(("RclMain::saveLastQuery: XML: [%s]\n", xml.c_str()));
string reason;
if (!stringtofile(xml, tofile.c_str(), reason)) {
QMessageBox::warning(this, tr("Write failed"),
tr("Could not write to file"));
}
return;
}
void RclMain::loadSavedQuery()
{
QString s =
QFileDialog::getOpenFileName(this, "Open saved query", prevDir(),
tr("Saved Queries (*.rclq)"));
if (s.isEmpty())
return;
string fromfile((const char *)s.toLocal8Bit());
string xml, reason;
if (!file_to_string(fromfile, xml, &reason)) {
QMessageBox::warning(this, tr("Read failed"),
tr("Could not open file: ") +
QString::fromUtf8(reason.c_str()));
return;
}
// Try to parse as SearchData
STD_SHARED_PTR<SearchData> sd = xmlToSearchData(xml);
if (sd) {
showAdvSearchDialog();
asearchform->fromSearch(sd);
return;
}
// Try to parse as Simple Search
SSearchDef sdef;
if (xmlToSSearch(xml, sdef)) {
if (sSearch->fromXML(sdef))
return;
}
QMessageBox::warning(this, tr("Load error"),
tr("Could not load saved query"));
}