File: ManagedFileInfo.cpp

package info (click to toggle)
taskjuggler 2.3.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,764 kB
  • ctags: 3,700
  • sloc: cpp: 36,852; sh: 12,761; xml: 5,541; perl: 5,207; makefile: 269; python: 258; lisp: 67
file content (164 lines) | stat: -rw-r--r-- 3,695 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
/*
 * The TaskJuggler Project Management Software
 *
 * Copyright (c) 2001, 2002, 2003, 2004, 2005 by Chris Schlaeger <cs@kde.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * $Id: ManagedFileInfo.cpp 1307 2006-07-19 13:09:16Z cs $
 */

#include "ManagedFileInfo.h"

#include <kmainwindow.h>
#include <klistview.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <kglobal.h>
#include <kconfig.h>
#include <kurl.h>
#include <kdebug.h>
#include <kiconloader.h>
#include <ktexteditor/document.h>
#include <ktexteditor/viewcursorinterface.h>
#include <ktexteditor/editinterface.h>
#include <kate/document.h>

#include "FileManager.h"

ManagedFileInfo::ManagedFileInfo(FileManager* fm, const KURL& url) :
    manager(fm), fileURL(url)
{
    editor = 0;
    modified = false;
    browserEntry = 0;
}

ManagedFileInfo::~ManagedFileInfo()
{/*
    if (editor)
    {
        KTextEditor::Document* doc = editor->document();
        delete editor;
        delete doc;
    }*/
}

void
ManagedFileInfo::setEditor(KTextEditor::View* e)
{
    editor = e;
    connect(editor->action("file_save"), SIGNAL(activated()),
            this, SLOT(fileSaved()));
}

void
ManagedFileInfo::readProperties(KConfig*)
{
}

void
ManagedFileInfo::writeProperties(KConfig*)
{
}

const KURL&
ManagedFileInfo::getFileURL() const
{
    return fileURL;
}

const QString
ManagedFileInfo::getUniqueName() const
{
    /* Return the file name that we use in the file browser. It may contain
     * path fragments to make it unique. */
    return browserEntry->text(0);
}

void
ManagedFileInfo::setModified()
{
    modified = TRUE;
    if (browserEntry)
        browserEntry->setPixmap(2, KGlobal::iconLoader()->loadIcon
                                ("filesaveas", KIcon::Small));
}

void
ManagedFileInfo::setModifiedOnDisc(Kate::Document* doc, bool,
                                   unsigned char reason)
{
    if (!editor || reason != 1)
        return;

    doc->reloadFile();
    modified = false;
}

QString
ManagedFileInfo::getWordUnderCursor() const
{
    unsigned int line, row;
    KTextEditor::viewCursorInterface(editor)->cursorPositionReal(&line, &row);
    QString textLine = KTextEditor::editInterface
        (editor->document())->textLine(line);
    uint start, length;
    for (start= row; textLine[start - 1].isLetter(); --start)
        ;
    for (length = 0; textLine[start + length].isLetter(); ++length)
        ;

    return textLine.mid(start, length);
}

void
ManagedFileInfo::save(bool ask)
{
    /* For some reason editor->document()->save() triggers a
     * KMainWindow::saveAll() which then ends up calling this function
     * recursively. To avoid this, we use a static flag to detect this
     * condition and avoid the recursive execution. */
    static bool rec = false;

    if (rec)
        return;

    rec = true;
    if (modified && editor)
    {
        if (ask && KMessageBox::warningYesNo
            (manager->getMainWindow(),
             i18n("The file %1 has unsaved modifications.\n"
                  "Do you want to save your modifications?")
             .arg(fileURL.url())) != KMessageBox::Yes)
            return;

        editor->document()->save();
        fileSaved();
    }
    rec = false;
}

void
ManagedFileInfo::saveAs(const KURL& url)
{
    if (editor)
    {
        editor->document()->saveAs(url);
        fileURL = url;
        fileSaved();
    }
}

void
ManagedFileInfo::fileSaved()
{
    modified = false;
    if (browserEntry)
        browserEntry->setPixmap(2, 0);
}

#include "ManagedFileInfo.moc"