summaryrefslogtreecommitdiffstats
path: root/konq-plugins/kuick/kdirmenu.cpp
blob: 1174ee3d208c1ab7e7e827f5709e7fad0c22c864 (plain)
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
/* This file is part of the KDE project
   Copyright (C) 2001 Holger Freyther <freyther@yahoo.com>
   Icon stroing inspired by Matthias Elters browser_mnu.* (currently not used)

   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; 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.
*/
#include <tqiconset.h>
#include <tqdir.h>
#include <tqfileinfo.h>

#include <kaction.h>
#include <kapplication.h>
#include <kiconloader.h>
#include <kio/global.h>
#include <klocale.h>
#include <kurl.h>
#include <ksimpleconfig.h>
#include "kdirmenu.h"

#define CICON(a) (*_icons)[a]

TQMap<TQString, TQPixmap> *KDirMenu::_icons = 0;

KDirMenu::KDirMenu ( TQWidget *tqparent, const KURL &_src,
            const TQString &_path, const TQString &_name, bool /*showfile*/ )
    : TQPopupMenu(tqparent),
      path(_path),
      name(_name),
      src( _src ),
      action( 0 )
{
    tqchildren.setAutoDelete( true );
    initIconMap( );
    connect( this, TQT_SIGNAL( aboutToShow( ) ), this, TQT_SLOT( slotAboutToShow( ) ) );
    connect( this, TQT_SIGNAL( aboutToHide( ) ), this, TQT_SLOT( slotAboutToHide( ) ) );
    tqchildren.clear(); // just in case

    TQFileInfo fileInfo(path);
    if (( src.path() != path || !src.isLocalFile()) && fileInfo.isWritable())
        action = new KAction(name, 0, TQT_TQOBJECT(this), TQT_SLOT(new_slot( ) ), TQT_TQOBJECT(this));
}
KDirMenu::~KDirMenu( ) {
    delete action;
    clear( );
    tqchildren.clear( );
}
void KDirMenu::insert( KDirMenu *submenu, const TQString &_path ) {
    static const TQIconSet folder = SmallIconSet("folder");
    TQString escapedPath = _path;
    TQString completPath=path+'/'+_path;
    // parse .directory if it does exist
    if (TQFile::exists(completPath + "/.directory")) {

        KSimpleConfig c(completPath + "/.directory", true);
        c.setDesktopGroup();
        TQString iconPath = c.readEntry("Icon");

        if ( iconPath.startsWith("./") )
           iconPath = _path + '/' + iconPath.mid(2);
        TQPixmap icon;
        icon = KGlobal::iconLoader()->loadIcon(iconPath,
                                               KIcon::Small, KIcon::SizeSmall,
                                               KIcon::DefaultState, 0, true);
        if(icon.isNull())
           icon = CICON("folder");
        insertItem( icon, escapedPath.tqreplace( "&", "&&" ), submenu );
    }
    else
        insertItem( folder, escapedPath.tqreplace( "&", "&&" ), submenu );
    tqchildren.append( submenu );
    connect(submenu, TQT_SIGNAL(fileChosen(const TQString &)),
            this, TQT_SLOT(slotFileSelected(const TQString &)));
}

void KDirMenu::slotAboutToShow( ) {

    // ok, prepare the dir: list all dirs and insert the new menus
    if (count() >= 1) return;

    //Precaution: if not a directory, exit, in case some path in KMetaMenu
    //isn't checked right
    if ( !TQFileInfo(path).isDir() )
        return;

    if ( action )
        action->plug( this );
    else
	setItemEnabled( insertItem( name ), false );

    // all dirs writeable and readable
    TQDir dir(path, TQString(),
             TQDir::Name | TQDir::DirsFirst | TQDir::IgnoreCase,
             TQDir::Dirs | TQDir::Readable | TQDir::Executable);

    const TQFileInfoList* dirList = dir.entryInfoList();
    if ( !dirList || dirList->isEmpty() ) {
        if ( action )
            action->setEnabled( false );
	return;
    }

    insertSeparator( );

    if (dirList->count() == 2) {
        insertItem(i18n("No Sub-Folders"), 0);
        setItemEnabled(0, false);
        return;
    }

    static const TQString& dot = KGlobal::staticQString( "." );
    static const TQString& dotdot = KGlobal::staticQString( ".." );

    for ( TQFileInfoListIterator it( *dirList ); *it; ++it ) {
        TQString fileName = (*it)->fileName();
        if ( fileName == dot || fileName == dotdot )
            continue;
            
        KURL u;
        u.setPath((*it)->absFilePath());
        if (kapp->authorizeURLAction("list", u, u))
        {
           insert(new KDirMenu(this, src, (*it)->absFilePath(), name),
			KIO::decodeFileName( fileName ));
	}
    }
}
void KDirMenu::slotAboutToHide( ) {

}
void KDirMenu::initIconMap()
{
    if(_icons) return;

//    kdDebug(90160) << "PanelBrowserMenu::initIconMap" << endl;

    _icons = new TQMap<TQString, TQPixmap>;

    _icons->insert("folder", SmallIcon("folder"));
    _icons->insert("unknown", SmallIcon("mime_empty"));
    _icons->insert("folder_open", SmallIcon("folder_open"));
    _icons->insert("kdisknav", SmallIcon("kdisknav"));
    _icons->insert("kfm", SmallIcon("kfm"));
    _icons->insert("terminal", SmallIcon("terminal"));
    _icons->insert("txt", SmallIcon("txt"));
    _icons->insert("exec", SmallIcon("exec"));
    _icons->insert("chardevice", SmallIcon("chardevice"));
}
void KDirMenu::slotFileSelected(const TQString &_path ){
    emit fileChosen( _path );
}

void KDirMenu::new_slot() {
    emit fileChosen(path );
}

#include "kdirmenu.moc"