2002-03-11 17:00:41 +00:00
|
|
|
/**
|
|
|
|
* \file xforms_helpers.C
|
2002-09-05 15:14:23 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-11 17:00:41 +00:00
|
|
|
*
|
2002-09-05 14:10:50 +00:00
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS
|
2000-11-02 14:47:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2002-04-23 08:16:27 +00:00
|
|
|
#ifdef __GNUG__
|
2000-11-02 14:47:47 +00:00
|
|
|
#pragma implementation
|
|
|
|
#endif
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-03-05 19:02:40 +00:00
|
|
|
#include "xforms_helpers.h"
|
2002-03-25 11:00:17 +00:00
|
|
|
|
2000-11-15 18:02:45 +00:00
|
|
|
#include "lyxlex.h"
|
|
|
|
#include "gettext.h"
|
2001-12-12 09:56:03 +00:00
|
|
|
#include "lyxlength.h"
|
2002-01-16 16:34:03 +00:00
|
|
|
#include "lyxgluelength.h"
|
2002-03-25 11:00:17 +00:00
|
|
|
|
2002-03-13 11:26:36 +00:00
|
|
|
#include "support/LAssert.h"
|
|
|
|
#include "support/FileInfo.h"
|
|
|
|
#include "support/filetools.h"
|
|
|
|
#include "support/lstrings.h" // frontStrip, strip
|
2000-11-02 14:47:47 +00:00
|
|
|
|
2002-03-25 11:00:17 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
2002-10-23 08:31:10 +00:00
|
|
|
|
2002-06-13 13:43:51 +00:00
|
|
|
#include FORMS_H_LOCATION
|
2002-03-25 11:00:17 +00:00
|
|
|
|
2000-11-15 18:02:45 +00:00
|
|
|
using std::ofstream;
|
|
|
|
using std::pair;
|
2000-11-02 14:47:47 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2002-10-22 12:39:05 +00:00
|
|
|
bool isActive(FL_OBJECT * ob)
|
|
|
|
{
|
|
|
|
return ob && ob->active > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-05 19:02:40 +00:00
|
|
|
// Set an FL_OBJECT to activated or deactivated
|
|
|
|
void setEnabled(FL_OBJECT * ob, bool enable)
|
|
|
|
{
|
|
|
|
if (enable) {
|
|
|
|
fl_activate_object(ob);
|
2002-10-23 08:31:10 +00:00
|
|
|
fl_set_object_lcol(ob, FL_LCOL);
|
2001-03-05 19:02:40 +00:00
|
|
|
} else {
|
|
|
|
fl_deactivate_object(ob);
|
|
|
|
fl_set_object_lcol(ob, FL_INACTIVE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
// Given an fl_choice or an fl_browser, create a vector of its entries
|
|
|
|
vector<string> const getVector(FL_OBJECT * ob)
|
2001-09-24 16:37:24 +00:00
|
|
|
{
|
2002-07-21 15:51:07 +00:00
|
|
|
vector <string> vec;
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
switch (ob->objclass) {
|
|
|
|
case FL_CHOICE:
|
|
|
|
for(int i = 0; i < fl_get_choice_maxitems(ob); ++i) {
|
|
|
|
string const text = fl_get_choice_item_text(ob, i+1);
|
2002-07-28 22:50:13 +00:00
|
|
|
vec.push_back(trim(text));
|
2002-07-21 15:51:07 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case FL_BROWSER:
|
|
|
|
for(int i = 0; i < fl_get_browser_maxline(ob); ++i) {
|
|
|
|
string const text = fl_get_browser_line(ob, i+1);
|
2002-07-28 22:50:13 +00:00
|
|
|
vec.push_back(trim(text));
|
2002-07-21 15:51:07 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lyx::Assert(0);
|
2001-09-24 16:37:24 +00:00
|
|
|
}
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2001-09-24 16:37:24 +00:00
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
///
|
|
|
|
string const getString(FL_OBJECT * ob, int line)
|
2002-01-08 10:05:53 +00:00
|
|
|
{
|
2002-10-23 08:31:10 +00:00
|
|
|
// Negative line value does not make sense.
|
|
|
|
lyx::Assert(line >= 0);
|
2002-07-21 15:51:07 +00:00
|
|
|
|
2002-10-23 08:31:10 +00:00
|
|
|
char const * tmp = 0;
|
2002-07-21 15:51:07 +00:00
|
|
|
switch (ob->objclass) {
|
|
|
|
case FL_INPUT:
|
|
|
|
tmp = fl_get_input(ob);
|
|
|
|
break;
|
2002-10-23 08:31:10 +00:00
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
case FL_BROWSER:
|
2002-10-23 08:31:10 +00:00
|
|
|
if (line == 0)
|
2002-07-21 15:51:07 +00:00
|
|
|
line = fl_get_browser(ob);
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
if (line >= 1 && line <= fl_get_browser_maxline(ob))
|
|
|
|
tmp = fl_get_browser_line(ob, line);
|
|
|
|
break;
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
case FL_CHOICE:
|
2002-10-23 08:31:10 +00:00
|
|
|
if (line == 0)
|
2002-07-21 15:51:07 +00:00
|
|
|
line = fl_get_choice(ob);
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
if (line >= 1 && line <= fl_get_choice_maxitems(ob))
|
|
|
|
tmp = fl_get_choice_item_text(ob, line);
|
|
|
|
break;
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
default:
|
|
|
|
lyx::Assert(0);
|
2002-07-28 22:50:13 +00:00
|
|
|
}
|
2002-07-21 15:51:07 +00:00
|
|
|
|
2002-10-23 08:31:10 +00:00
|
|
|
return tmp ? trim(tmp) : string();
|
2002-01-08 10:05:53 +00:00
|
|
|
}
|
2002-07-28 22:50:13 +00:00
|
|
|
|
2001-10-09 12:36:36 +00:00
|
|
|
string getLengthFromWidgets(FL_OBJECT * input, FL_OBJECT * choice)
|
|
|
|
{
|
|
|
|
// Paranoia check
|
|
|
|
lyx::Assert(input && input->objclass == FL_INPUT &&
|
|
|
|
choice && choice->objclass == FL_CHOICE);
|
|
|
|
|
2002-07-28 22:50:13 +00:00
|
|
|
string const length = trim(fl_get_input(input));
|
2001-10-09 15:20:10 +00:00
|
|
|
if (length.empty())
|
2001-10-11 17:28:28 +00:00
|
|
|
return string();
|
2001-10-09 12:36:36 +00:00
|
|
|
|
2002-10-23 08:31:10 +00:00
|
|
|
// don't return unit-from-choice if the input(field) contains a unit
|
2002-01-16 16:34:03 +00:00
|
|
|
if (isValidGlueLength(length))
|
|
|
|
return length;
|
|
|
|
|
2002-07-28 22:50:13 +00:00
|
|
|
string unit = trim(fl_get_choice_text(choice));
|
2001-10-15 10:28:31 +00:00
|
|
|
unit = subst(unit, "%%", "%");
|
2001-10-09 12:36:36 +00:00
|
|
|
|
2001-10-15 10:28:31 +00:00
|
|
|
return length + unit;
|
2001-10-09 12:36:36 +00:00
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2001-10-09 12:36:36 +00:00
|
|
|
|
2001-12-11 17:26:52 +00:00
|
|
|
void updateWidgetsFromLengthString(FL_OBJECT * input, FL_OBJECT * choice,
|
|
|
|
string const & str,
|
|
|
|
string const & default_unit)
|
|
|
|
{
|
2002-02-11 18:57:30 +00:00
|
|
|
// use input field only for gluelengths
|
|
|
|
if (!isValidLength(str) && !isStrDbl(str)) {
|
|
|
|
fl_set_input(input, str.c_str());
|
2002-03-22 11:41:23 +00:00
|
|
|
// we assume that "default_unit" is in the choice as "we"
|
|
|
|
// have control over that!
|
2002-10-09 14:38:18 +00:00
|
|
|
// No need to check for its presence in the choice, therefore.
|
2002-02-11 18:57:30 +00:00
|
|
|
fl_set_choice_text(choice, default_unit.c_str());
|
|
|
|
} else {
|
|
|
|
updateWidgetsFromLength(input, choice,
|
2001-12-11 17:26:52 +00:00
|
|
|
LyXLength(str), default_unit);
|
2002-02-11 18:57:30 +00:00
|
|
|
}
|
2001-12-11 17:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void updateWidgetsFromLength(FL_OBJECT * input, FL_OBJECT * choice,
|
|
|
|
LyXLength const & len,
|
|
|
|
string const & default_unit)
|
|
|
|
{
|
|
|
|
// Paranoia check
|
|
|
|
lyx::Assert(input && input->objclass == FL_INPUT &&
|
|
|
|
choice && choice->objclass == FL_CHOICE);
|
|
|
|
|
2002-12-04 02:57:14 +00:00
|
|
|
if (len.empty()) {
|
2001-12-11 17:26:52 +00:00
|
|
|
fl_set_input(input, "");
|
|
|
|
fl_set_choice_text(choice, default_unit.c_str());
|
|
|
|
} else {
|
|
|
|
ostringstream buffer;
|
|
|
|
buffer << len.value();
|
|
|
|
fl_set_input(input, buffer.str().c_str());
|
2002-03-22 11:41:23 +00:00
|
|
|
|
|
|
|
// Set the choice to the desired unit, if present in the choice.
|
|
|
|
// Else set the choice to the default unit.
|
|
|
|
string const unit = subst(stringFromUnit(len.unit()),"%","%%");
|
|
|
|
|
2002-07-21 15:51:07 +00:00
|
|
|
vector<string> const vec = getVector(choice);
|
2002-03-22 11:41:23 +00:00
|
|
|
vector<string>::const_iterator it =
|
|
|
|
std::find(vec.begin(), vec.end(), unit);
|
|
|
|
if (it != vec.end()) {
|
|
|
|
fl_set_choice_text(choice, unit.c_str());
|
|
|
|
} else {
|
|
|
|
fl_set_choice_text(choice, default_unit.c_str());
|
|
|
|
}
|
2001-12-11 17:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-11-02 14:47:47 +00:00
|
|
|
// Take a string and add breaks so that it fits into a desired label width, w
|
2000-12-29 12:48:02 +00:00
|
|
|
string formatted(string const & sin, int w, int size, int style)
|
2000-11-02 14:47:47 +00:00
|
|
|
{
|
|
|
|
string sout;
|
2000-12-29 12:48:02 +00:00
|
|
|
if (sin.empty()) return sout;
|
2000-11-02 14:47:47 +00:00
|
|
|
|
2002-03-18 15:56:00 +00:00
|
|
|
string::size_type curpos = 0;
|
|
|
|
string line;
|
|
|
|
for(;;) {
|
|
|
|
string::size_type const nxtpos1 = sin.find(' ', curpos);
|
|
|
|
string::size_type const nxtpos2 = sin.find('\n', curpos);
|
2002-10-09 14:38:18 +00:00
|
|
|
string::size_type const nxtpos = std::min(nxtpos1, nxtpos2);
|
2002-03-18 15:56:00 +00:00
|
|
|
|
|
|
|
string const word = nxtpos == string::npos ?
|
|
|
|
sin.substr(curpos) : sin.substr(curpos, nxtpos-curpos);
|
|
|
|
|
|
|
|
bool const newline = (nxtpos2 != string::npos &&
|
|
|
|
nxtpos2 < nxtpos1);
|
|
|
|
|
|
|
|
string const line_plus_word =
|
|
|
|
line.empty() ? word : line + ' ' + word;
|
|
|
|
|
|
|
|
int const length =
|
|
|
|
fl_get_string_width(style, size,
|
|
|
|
line_plus_word.c_str(),
|
|
|
|
int(line_plus_word.length()));
|
|
|
|
|
|
|
|
if (length >= w) {
|
|
|
|
sout += line + '\n';
|
|
|
|
if (newline) {
|
|
|
|
sout += word + '\n';
|
|
|
|
line.erase();
|
|
|
|
} else {
|
|
|
|
line = word;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (newline) {
|
|
|
|
sout += line_plus_word + '\n';
|
|
|
|
line.erase();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (!line.empty())
|
|
|
|
line += ' ';
|
|
|
|
line += word;
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2002-03-18 15:56:00 +00:00
|
|
|
if (nxtpos == string::npos) {
|
|
|
|
if (!line.empty())
|
|
|
|
sout += line;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
curpos = nxtpos+1;
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2000-11-02 14:47:47 +00:00
|
|
|
return sout;
|
|
|
|
}
|
2000-11-15 18:02:45 +00:00
|
|
|
|
|
|
|
|
2002-03-13 11:26:36 +00:00
|
|
|
void setCursorColor(int color)
|
|
|
|
{
|
|
|
|
fl_set_cursor_color(FL_DEFAULT_CURSOR, color, FL_WHITE);
|
|
|
|
fl_set_cursor_color(XC_xterm, color, FL_WHITE);
|
|
|
|
fl_set_cursor_color(XC_watch, color, FL_WHITE);
|
|
|
|
fl_set_cursor_color(XC_sb_right_arrow, color, FL_WHITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
namespace {
|
|
|
|
|
2000-11-15 18:02:45 +00:00
|
|
|
// sorted by hand to prevent LyXLex from complaining on read().
|
|
|
|
keyword_item xformTags[] = {
|
2002-03-11 09:54:42 +00:00
|
|
|
{ "\\gui_background", FL_COL1 },
|
2000-11-15 18:02:45 +00:00
|
|
|
{ "\\gui_buttonbottom", FL_BOTTOM_BCOL },
|
2002-03-11 09:54:42 +00:00
|
|
|
{ "\\gui_buttonleft", FL_LEFT_BCOL },
|
|
|
|
{ "\\gui_buttonright", FL_RIGHT_BCOL },
|
|
|
|
{ "\\gui_buttontop", FL_TOP_BCOL },
|
|
|
|
{ "\\gui_inactive", FL_INACTIVE },
|
|
|
|
{ "\\gui_pointer", FL_FREE_COL16 },
|
|
|
|
{ "\\gui_push_button", FL_YELLOW },
|
2002-03-21 21:21:28 +00:00
|
|
|
{ "\\gui_selected", FL_MCOL },
|
2002-03-11 09:54:42 +00:00
|
|
|
{ "\\gui_text", FL_BLACK }
|
2000-11-15 18:02:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-03-20 01:22:46 +00:00
|
|
|
const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
|
|
|
|
|
|
|
|
} // namespace anon
|
2000-11-15 18:02:45 +00:00
|
|
|
|
|
|
|
|
2001-03-05 19:02:40 +00:00
|
|
|
bool XformsColor::read(string const & filename)
|
2000-11-15 18:02:45 +00:00
|
|
|
{
|
|
|
|
LyXLex lexrc(xformTags, xformCount);
|
|
|
|
if (!lexrc.setFile(filename))
|
|
|
|
return false;
|
|
|
|
|
2001-08-06 19:13:25 +00:00
|
|
|
while (lexrc.isOK()) {
|
2000-11-15 18:02:45 +00:00
|
|
|
int const le = lexrc.lex();
|
|
|
|
|
|
|
|
switch (le) {
|
|
|
|
case LyXLex::LEX_UNDEF:
|
|
|
|
lexrc.printError("Unknown tag `$$Token'");
|
2002-03-21 21:21:28 +00:00
|
|
|
continue;
|
2000-11-15 18:02:45 +00:00
|
|
|
case LyXLex::LEX_FEOF:
|
|
|
|
continue;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
2002-03-11 09:54:42 +00:00
|
|
|
string const tag = lexrc.getString();
|
|
|
|
|
2000-11-15 18:02:45 +00:00
|
|
|
RGBColor col;
|
|
|
|
|
|
|
|
if (!lexrc.next()) break;
|
2001-08-06 19:13:25 +00:00
|
|
|
col.r = lexrc.getInteger();
|
2000-11-15 18:02:45 +00:00
|
|
|
|
|
|
|
if (!lexrc.next()) break;
|
2001-08-06 19:13:25 +00:00
|
|
|
col.g = lexrc.getInteger();
|
2000-11-15 18:02:45 +00:00
|
|
|
|
|
|
|
if (!lexrc.next()) break;
|
2001-08-06 19:13:25 +00:00
|
|
|
col.b = lexrc.getInteger();
|
2000-11-15 18:02:45 +00:00
|
|
|
|
|
|
|
fl_mapcolor(le, col.r, col.g, col.b);
|
2002-03-11 09:54:42 +00:00
|
|
|
|
|
|
|
if (tag == "\\gui_pointer") {
|
2002-03-13 11:26:36 +00:00
|
|
|
setCursorColor(FL_FREE_COL16);
|
2002-03-11 09:54:42 +00:00
|
|
|
}
|
2000-11-15 18:02:45 +00:00
|
|
|
}
|
2002-03-13 11:26:36 +00:00
|
|
|
|
2000-11-15 18:02:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-03-05 19:02:40 +00:00
|
|
|
bool XformsColor::write(string const & filename)
|
2000-11-15 18:02:45 +00:00
|
|
|
{
|
|
|
|
ofstream os(filename.c_str());
|
|
|
|
if (!os)
|
|
|
|
return false;
|
|
|
|
|
2002-09-05 14:10:50 +00:00
|
|
|
os << "###"
|
|
|
|
<< "### file " << filename << "\n\n"
|
|
|
|
<< "### This file is written by LyX, if you want to make your own\n"
|
|
|
|
<< "### modifications you should do them from inside LyX and save\n"
|
2002-11-27 10:30:28 +00:00
|
|
|
<< '\n';
|
2000-11-15 18:02:45 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < xformCount; ++i) {
|
|
|
|
string const tag = xformTags[i].tag;
|
|
|
|
int const colorID = xformTags[i].code;
|
|
|
|
RGBColor color;
|
|
|
|
|
|
|
|
fl_getmcolor(colorID, &color.r, &color.g, &color.b);
|
|
|
|
|
2002-11-27 10:30:28 +00:00
|
|
|
os << tag << ' '
|
|
|
|
<< color.r << ' ' << color.g << ' ' << color.b << '\n';
|
2000-11-15 18:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2000-11-21 15:46:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
string RWInfo::error_message;
|
|
|
|
|
|
|
|
bool RWInfo::WriteableDir(string const & name)
|
|
|
|
{
|
|
|
|
error_message.erase();
|
|
|
|
|
|
|
|
if (!AbsolutePath(name)) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("The absolute path is required.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo const tp(name);
|
2002-01-07 10:17:44 +00:00
|
|
|
if (!tp.isOK() || !tp.isDir()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("Directory does not exist.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tp.writable()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("Cannot write to this directory.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RWInfo::ReadableDir(string const & name)
|
|
|
|
{
|
|
|
|
error_message.erase();
|
|
|
|
|
|
|
|
if (!AbsolutePath(name)) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("The absolute path is required.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo const tp(name);
|
2002-01-07 10:17:44 +00:00
|
|
|
if (!tp.isOK() || !tp.isDir()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("Directory does not exist.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!tp.readable()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("Cannot read this directory.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RWInfo::WriteableFile(string const & name)
|
|
|
|
{
|
|
|
|
// A writeable file is either:
|
|
|
|
// * An existing file to which we have write access, or
|
|
|
|
// * A file that doesn't yet exist but that would exist in a writeable
|
|
|
|
// directory.
|
|
|
|
|
|
|
|
error_message.erase();
|
|
|
|
|
|
|
|
if (name.empty()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("No file input.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string const dir = OnlyPath(name);
|
|
|
|
if (!AbsolutePath(dir)) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("The absolute path is required.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo d(name);
|
2002-01-07 10:17:44 +00:00
|
|
|
|
|
|
|
if (!d.isOK() || !d.isDir()) {
|
2000-11-21 15:46:13 +00:00
|
|
|
d.newFile(dir);
|
|
|
|
}
|
|
|
|
|
2002-01-07 17:57:21 +00:00
|
|
|
if (!d.isOK() || !d.isDir()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("Directory does not exist.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2000-11-21 15:46:13 +00:00
|
|
|
if (!d.writable()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("Cannot write to this directory.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo f(name);
|
2002-01-07 10:17:44 +00:00
|
|
|
if (dir == name || (f.isOK() && f.isDir())) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("A file is required, not a directory.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2002-01-07 10:17:44 +00:00
|
|
|
if (f.isOK() && f.exist() && !f.writable()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("Cannot write to this file.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2000-11-21 15:46:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool RWInfo::ReadableFile(string const & name)
|
|
|
|
{
|
|
|
|
error_message.erase();
|
|
|
|
|
|
|
|
if (name.empty()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("No file input.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
string const dir = OnlyPath(name);
|
|
|
|
if (!AbsolutePath(dir)) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("The absolute path is required.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo d(name);
|
2002-01-07 17:57:21 +00:00
|
|
|
|
2002-01-07 10:17:44 +00:00
|
|
|
if (!d.isOK() && !d.isDir()) {
|
2000-11-21 15:46:13 +00:00
|
|
|
d.newFile(dir);
|
|
|
|
}
|
|
|
|
|
2002-01-07 17:57:21 +00:00
|
|
|
if (!d.isOK() || !d.isDir()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("Directory does not exist.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2000-11-21 15:46:13 +00:00
|
|
|
if (!d.readable()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("Cannot read from this directory.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileInfo f(name);
|
2002-01-07 10:17:44 +00:00
|
|
|
if (dir == name || (f.isOK() && f.isDir())) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("A file is required, not a directory.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!f.exist()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("File does not exist.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2002-03-21 21:21:28 +00:00
|
|
|
|
2000-11-21 15:46:13 +00:00
|
|
|
if (!f.readable()) {
|
2002-04-02 17:04:23 +00:00
|
|
|
error_message = _("Cannot read from this file.");
|
2000-11-21 15:46:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|