Import sample fragbuts.xml if user file does not exist

This commit is contained in:
Jean-Francois Dockes 2015-03-28 08:43:45 +01:00
parent 5c067685f0
commit 895f7c1ab1
4 changed files with 58 additions and 13 deletions

View file

@ -36,6 +36,7 @@
#include "recoll.h"
#include "debuglog.h"
#include "readfile.h"
#include "copyfile.h"
using namespace std;
@ -147,6 +148,12 @@ FragButs::FragButs(QWidget* parent)
m_fn = path_cat(theconfig->getConfDir(), "fragbuts.xml");
string data, reason;
if (access(m_fn.c_str(), 0) != 0) {
// config does not exist: try to create it from sample
string src = path_cat(theconfig->getDatadir(), "examples");
src = path_cat(src, "fragbuts.xml");
copyfile(src.c_str(), m_fn.c_str(), reason);
}
if (!file_to_string(m_fn, data, &reason)) {
QMessageBox::warning(0, "Recoll",
tr("%1 not found.").arg(

View file

@ -1,8 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Example file for configuring the "Query fragments" window.
Each <buttons> or <radiobuttons> element defines a lines of buttons
Each active button adds a query language fragment to the current query
before execution.
-->
<fragbuts version="1.0">
<radiobuttons>
<!-- Actually useful: toggle WEB queue results inclusion -->
<fragbut>
<label>Include Web Results</label>
<frag></frag>
@ -23,20 +29,29 @@
<buttons>
<fragbut>
<label>Year 2010</label>
<label>Example: Year 2010</label>
<frag>date:2010-01-01/2010-12-31</frag>
</fragbut>
<fragbut>
<label>c++ files</label>
<label>Example: c++ files</label>
<frag>ext:cpp OR ext:cxx</frag>
</fragbut>
<fragbut>
<label>My Great Directory</label>
<label>Example: My Great Directory</label>
<frag>dir:/my/great/directory</frag>
</fragbut>
</buttons>
<buttons>
<fragbut>
<label>Check the manual (Query Fragments Window section) for explanations</label>
<frag></frag>
</fragbut>
</buttons>
</fragbuts>

View file

@ -29,8 +29,9 @@
#include "copyfile.h"
#include "debuglog.h"
using namespace std;
#define CPBSIZ 8192
#define COPYFILE_NOERRUNLINK 1
bool copyfile(const char *src, const char *dst, string &reason, int flags)
{
@ -38,6 +39,7 @@ bool copyfile(const char *src, const char *dst, string &reason, int flags)
int dfd = -1;
bool ret = false;
char buf[CPBSIZ];
int oflags = O_WRONLY|O_CREAT|O_TRUNC;
LOGDEB(("copyfile: %s to %s\n", src, dst));
@ -46,7 +48,11 @@ bool copyfile(const char *src, const char *dst, string &reason, int flags)
goto out;
}
if ((dfd = open(dst, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) {
if (flags & COPYFILE_EXCL) {
oflags |= O_EXCL;
}
if ((dfd = open(dst, oflags, 0644)) < 0) {
reason += string("open/creat ") + dst + ": " + strerror(errno);
// If we fail because of an open/truncate error, we do not want to unlink
// the file, we might succeed...
@ -143,18 +149,21 @@ bool renameormove(const char *src, const char *dst, string &reason)
#include <string>
#include <iostream>
using namespace std;
#include "copyfile.h"
using namespace std;
static int op_flags;
#define OPT_MOINS 0x1
#define OPT_m 0x2
#define OPT_e 0x4
static const char *thisprog;
static char usage [] =
"trcopyfile [-m] src dst\n"
" -m : move instead of copying\n"
" -e : fail if dest exists (only for copy)\n"
"\n"
;
static void
@ -177,6 +186,7 @@ int main(int argc, const char **argv)
while (**argv)
switch (*(*argv)++) {
case 'm': op_flags |= OPT_m; break;
case 'e': op_flags |= OPT_e; break;
default: Usage(); break;
}
argc--; argv++;
@ -191,7 +201,11 @@ int main(int argc, const char **argv)
if (op_flags & OPT_m) {
ret = renameormove(src.c_str(), dst.c_str(), reason);
} else {
ret = copyfile(src.c_str(), dst.c_str(), reason);
int flags = 0;
if (op_flags & OPT_e) {
flags |= COPYFILE_EXCL;
}
ret = copyfile(src.c_str(), dst.c_str(), reason, flags);
}
if (!ret) {
cerr << reason << endl;

View file

@ -18,14 +18,23 @@
#define _COPYFILE_H_INCLUDED_
#include <string>
using std::string;
enum CopyfileFlags {COPYFILE_NONE = 0, COPYFILE_NOERRUNLINK = 1};
enum CopyfileFlags {COPYFILE_NONE = 0,
COPYFILE_NOERRUNLINK = 1,
COPYFILE_EXCL = 2,
};
extern bool copyfile(const char *src, const char *dst, string &reason,
/** Copy src to dst.
*
* We destroy an existing dst except if COPYFILE_EXCL is set (or we if
* have no permission...).
* A partially copied dst is normally removed, except if COPYFILE_NOERRUNLINK
* is set.
*/
extern bool copyfile(const char *src, const char *dst, std::string &reason,
int flags = 0);
// Try to rename, copy/unlink source if this fails (different devs)
extern bool renameormove(const char *src, const char *dst, string &reason);
/** Try to rename src. If this fails (different devices) copy then unlink src */
extern bool renameormove(const char *src, const char *dst, std::string &reason);
#endif /* _COPYFILE_H_INCLUDED_ */