File: information.cpp

package info (click to toggle)
calligra 1%3A2.4.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 290,028 kB
  • sloc: cpp: 1,105,019; xml: 24,940; ansic: 11,807; python: 8,457; perl: 2,792; sh: 1,507; yacc: 1,307; ruby: 1,248; sql: 903; lex: 455; makefile: 89
file content (383 lines) | stat: -rw-r--r-- 11,355 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* This file is part of the KDE project
   Copyright (C) 1998-2002 The KSpread Team <calligra-devel@kde.org>
   Copyright (C) 2005 Tomas Mecir <mecirt@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; only
   version 2 of the License.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

// built-in information functions

#include "InformationModule.h"

#include <calligraversion.h>
#include <sys/utsname.h>

#include <QDir>
#include <kdebug.h>
#include <KLocale>

#include "CalculationSettings.h"
#include "Function.h"
#include "FunctionModuleRegistry.h"
#include "ValueCalc.h"
#include "ValueConverter.h"
#include "Map.h"
#include "Sheet.h"
#include "Region.h"
#include "Cell.h"
#include "Formula.h"
#include "CellStorage.h"

#include <KoDocument.h>

using namespace Calligra::Sheets;

// prototypes (sorted alphabetically)
Value func_errortype(valVector args, ValueCalc *calc, FuncExtra *);
Value func_filename(valVector args, ValueCalc *calc, FuncExtra *);
Value func_formula(valVector args, ValueCalc *calc, FuncExtra *);
Value func_info(valVector args, ValueCalc *calc, FuncExtra *);
Value func_isblank(valVector args, ValueCalc *calc, FuncExtra *);
Value func_isdate(valVector args, ValueCalc *calc, FuncExtra *);
Value func_iserr(valVector args, ValueCalc *calc, FuncExtra *);
Value func_iserror(valVector args, ValueCalc *calc, FuncExtra *);
Value func_iseven(valVector args, ValueCalc *calc, FuncExtra *);
Value func_isformula(valVector args, ValueCalc *calc, FuncExtra *);
Value func_islogical(valVector args, ValueCalc *calc, FuncExtra *);
Value func_isna(valVector args, ValueCalc *calc, FuncExtra *);
Value func_isnottext(valVector args, ValueCalc *calc, FuncExtra *);
Value func_isnum(valVector args, ValueCalc *calc, FuncExtra *);
Value func_isodd(valVector args, ValueCalc *calc, FuncExtra *);
Value func_isref(valVector args, ValueCalc *calc, FuncExtra *);
Value func_istext(valVector args, ValueCalc *calc, FuncExtra *);
Value func_istime(valVector args, ValueCalc *calc, FuncExtra *);
Value func_n(valVector args, ValueCalc *calc, FuncExtra *);
Value func_na(valVector args, ValueCalc *calc, FuncExtra *);
Value func_type(valVector args, ValueCalc *calc, FuncExtra *);
Value func_version(valVector args, ValueCalc *calc, FuncExtra *);

CALLIGRA_SHEETS_EXPORT_FUNCTION_MODULE("information", InformationModule)


InformationModule::InformationModule(QObject* parent, const QVariantList&)
        : FunctionModule(parent)
{
    Function *f;

    f = new Function("ERRORTYPE", func_errortype);
    add(f);
    f = new Function("FILENAME", func_filename);
    f->setParamCount(0);
    add(f);
    f = new Function("FORMULA", func_formula);
    f->setParamCount(1);
    f->setNeedsExtra(true);
    add(f);
    f = new Function("INFO", func_info);
    add(f);
    f = new Function("ISBLANK", func_isblank);
    add(f);
    f = new Function("ISDATE", func_isdate);
    add(f);
    f = new Function("ISERR", func_iserr);
    add(f);
    f = new Function("ISERROR", func_iserror);
    add(f);
    f = new Function("ISEVEN", func_iseven);
    f->setAlternateName("COM.SUN.STAR.SHEET.ADDIN.ANALYSIS.GETISEVEN");
    add(f);
    f = new Function("ISFORMULA", func_isformula);
    f->setNeedsExtra(true);
    add(f);
    f = new Function("ISLOGICAL", func_islogical);
    add(f);
    f = new Function("ISNA", func_isna);
    add(f);
    f = new Function("ISNONTEXT", func_isnottext);
    add(f);
    f = new Function("ISNOTTEXT", func_isnottext);
    add(f);
    f = new Function("ISNUM", func_isnum);
    add(f);
    f = new Function("ISNUMBER", func_isnum);
    add(f);
    f = new Function("ISODD", func_isodd);
    f->setAlternateName("COM.SUN.STAR.SHEET.ADDIN.ANALYSIS.GETISODD");
    add(f);
    f = new Function("ISREF", func_isref);
    f->setNeedsExtra(true);
    f->setAcceptArray();
    add(f);
    f = new Function("ISTEXT", func_istext);
    add(f);
    f = new Function("ISTIME", func_istime);
    add(f);
    f = new Function("N", func_n);
    add(f);
    f = new Function("NA", func_na);
    f->setParamCount(0);
    add(f);
    f = new Function("TYPE", func_type);
    f->setAcceptArray();
    add(f);
}

QString InformationModule::descriptionFileName() const
{
    return QString("information.xml");
}

// Function: ERROR.TYPE
Value func_errortype(valVector args, ValueCalc *, FuncExtra *)
{
    if (! args[0].isError()) {
        // its an error if the argument isn't an error...
        return Value::errorVALUE();
    }

    if (args[0] == Value::errorNULL()) {
        return Value(1);
    } else if (args[0] == Value::errorDIV0()) {
        return Value(2);
    } else if (args[0] == Value::errorVALUE()) {
        return Value(3);
    } else if (args[0] == Value::errorREF()) {
        return Value(4);
    } else if (args[0] == Value::errorNAME()) {
        return Value(5);
    } else if (args[0] == Value::errorNUM()) {
        return Value(6);
    } else if (args[0] == Value::errorNA()) {
        return Value(7);
    } else if (args[0] == Value::errorCIRCLE()) {
        // non-standard error type
        return Value(101);
    } else if (args[0] == Value::errorDEPEND()) {
        // non-standard error type
        return Value(102);
    } else if (args[0] == Value::errorPARSE()) {
        // non-standard error type
        return Value(103);
    } else {
        // something I didn't think of...
        kDebug() << "Unexpected error type";
        return Value(0);
    }
}
// Function: INFO
Value func_info(valVector args, ValueCalc *calc, FuncExtra *)
{
    QString type = calc->conv()->asString(args[0]).asString().toLower();

    if (type == "directory")
        return Value(QDir::currentPath());

    if (type == "release")
        return Value(QString(CALLIGRA_VERSION_STRING));

    if (type == "numfile")
        return Value(KoDocument::documentList() ? KoDocument::documentList()->count() : 0);

    if (type == "recalc") {
        QString result;
        if (!calc->settings()->isAutoCalculationEnabled())
            result = i18n("Manual");
        else
            result = i18n("Automatic");
        return Value(result);
    }

    if (type == "memavail")
        // not supported
        return Value::errorVALUE();
    if (type == "memused")
        // not supported
        return Value::errorVALUE();
    if (type == "origin")
        // not supported
        return Value::errorVALUE();

    if (type == "system") {
        struct utsname name;
        if (uname(&name) >= 0)
            return Value(QString(name.sysname));
    }

    if (type == "totmem")
        // not supported
        return Value::errorVALUE();

    if (type == "osversion") {
        struct utsname name;
        if (uname(&name) >= 0) {
            QString os = QString("%1 %2 (%3)").arg(name.sysname).
                         arg(name.release).arg(name.machine);
            return Value(os);
        }
    }

    return Value::errorVALUE();
}

// Function: ISBLANK
Value func_isblank(valVector args, ValueCalc *, FuncExtra *)
{
    return Value(args[0].isEmpty());
}

// Function: ISLOGICAL
Value func_islogical(valVector args, ValueCalc *, FuncExtra *)
{
    return Value(args[0].isBoolean());
}

// Function: ISTEXT
Value func_istext(valVector args, ValueCalc *, FuncExtra *)
{
    return Value(args[0].isString());
}

// Function: ISREF
Value func_isref(valVector args, ValueCalc * /*calc*/, FuncExtra *e)
{
    if (args[0].isError()) return args[0];  // errors pass through
    // no reference ?
    if ((e == 0) || (e->ranges[0].col1 == -1) || (e->ranges[0].row1 == -1))
        return Value(false);
    // if we are here, it is a reference (cell/range)
    return Value(true);
}

// Function: ISNOTTEXT
Value func_isnottext(valVector args, ValueCalc *, FuncExtra *)
{
    return Value(args[0].isString() ? false : true);
}

// Function: ISNUM
Value func_isnum(valVector args, ValueCalc *, FuncExtra *)
{
    return Value(args[0].isNumber());
}

// Function: ISTIME
Value func_istime(valVector args, ValueCalc *, FuncExtra *)
{
    return Value((args[0].format() == Value::fmt_Time)
                 || (args[0].format() == Value::fmt_DateTime));
}

// Function: ISDATE
Value func_isdate(valVector args, ValueCalc *, FuncExtra *)
{
    return Value((args[0].format() == Value::fmt_Date)
                 || (args[0].format() == Value::fmt_DateTime));
}

// Function: ISODD
Value func_isodd(valVector args, ValueCalc *calc, FuncExtra *)
{
    return Value(calc->isEven(args[0]) ? false : true);
}

// Function: ISEVEN
Value func_iseven(valVector args, ValueCalc *calc, FuncExtra *)
{
    if (args[0].isError())
        return args[0];
    return Value(calc->isEven(args[0]));
}

// Function: ISFORMULA
Value func_isformula(valVector args, ValueCalc *calc, FuncExtra *e)
{
    const Calligra::Sheets::Region &region = e->regions[0];
    QPoint p = region.firstRange().topLeft();
    CellStorage *s = region.firstSheet()->cellStorage();
    Formula formula = s->formula(p.x(), p.y());
    return Value(formula.isValid());
}

// Function: ISERR
Value func_iserr(valVector args, ValueCalc *, FuncExtra *)
{
    return Value((args[0].isError() &&
                  (args[0].errorMessage() != Value::errorNA().errorMessage())));
}

// Function: ISERROR
Value func_iserror(valVector args, ValueCalc *, FuncExtra *)
{
    return Value(args[0].isError());
}

// Function: ISNA
Value func_isna(valVector args, ValueCalc *, FuncExtra *)
{
    return Value((args[0].isError() &&
                  (args[0].errorMessage() == Value::errorNA().errorMessage())));
}

// Function: TYPE
Value func_type(valVector args, ValueCalc *, FuncExtra *)
{
    // Returns 1 for numbers, 2 for text, 4 for boolean, 16 for error,
    // 64 for arrays
    if (args[0].isArray())
        return Value(64);
    if (args[0].isNumber())
        return Value(1);
    if (args[0].isString())
        return Value(2);
    if (args[0].isBoolean())
        return Value(4);
    if (args[0].isError())
        return Value(16);

    // something else ?
    return Value(0);
}

Value func_filename(valVector, ValueCalc *calc, FuncExtra *)
{
    return Value(calc->settings()->fileName());
}

Value func_formula(valVector, ValueCalc *, FuncExtra *e)
{
    if(e->ranges[0].col1 < 1 || e->ranges[0].row1 < 1)
        return Value::errorVALUE();
    const Calligra::Sheets::Cell c(e->sheet, e->ranges[0].col1, e->ranges[0].row1);
    if (c.isNull())
        return Value::errorVALUE();
    if (!c.isFormula())
        return Value::errorNA();
    return Value(c.formula().expression());
}

// Function: N
Value func_n(valVector args, ValueCalc *calc, FuncExtra *)
{
    return calc->conv()->asFloat(args[0]);
}

// Function: NA
Value func_na(valVector, ValueCalc *, FuncExtra *)
{
    return Value::errorNA();
}

#include "InformationModule.moc"