summaryrefslogtreecommitdiffstats
path: root/konq-plugins/kuick/kmetamenu.cpp
blob: 065a825d031038c48e7e8e40872bd8fc5ba0f1b0 (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
170
171
172
173
174
175
176
177
178
179
180
/* This file is part of the KDE project
   Copyright (C) 2001 Holger Freyther <[email protected]>

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

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

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

#include <kiconloader.h>
#include <kimproxy.h>
#include <kapplication.h>
#include <klocale.h>
#include <kconfig.h>
#include <kfiledialog.h>
#include <kurl.h>
#include <konq_popupmenu.h>

#include <qpixmap.h>
#include <qdir.h>
#include <qiconset.h>
#include <qstringlist.h>

#include "kmetamenu.h"
#include "kdirmenu.h"
#include "kimcontactmenu.h"
#include "kmetamenu.moc"

KMetaMenu::KMetaMenu( QWidget *parent, const KURL &url,
        const QString &text, const QString &key, KIMProxy *imProxy )
: QPopupMenu( parent),
  m_root( 0 ), m_home( 0 ), m_etc( 0 ), m_current( 0 ), m_browse( 0 ) {
     int recent_no;
     group = key;
     actions.setAutoDelete( TRUE );

     QStringList dirList;

     KURL u;

     u.setPath(QDir::homeDirPath());
     if ( kapp->authorizeURLAction("list", u, u) )
     {
         m_home = new KDirMenu( parent, url, u.path() , text );
         insertItem( SmallIcon( "kfm_home" ), i18n("&Home Folder"), m_home);
         dirList << u.path();

         connect(m_home, SIGNAL(fileChosen(const QString &)),
                 SLOT(slotFileChosen(const QString &) ) );
     }

     u.setPath(QDir::rootDirPath());
     if ( kapp->authorizeURLAction("list", u, u) )
     {
         m_root = new KDirMenu( parent, url, u.path() , text );
         insertItem( SmallIcon( "folder_red" ), i18n("&Root Folder"), m_root);
         dirList << u.path();

         connect(m_root, SIGNAL(fileChosen(const QString &)),
                 SLOT(slotFileChosen(const QString &) ) );
     }

     QString confDir = QDir::rootDirPath()+ "etc";
     u.setPath(confDir);
     if ( QFileInfo( confDir ).isWritable() &&
          kapp->authorizeURLAction("list", u, u) )
     {
         m_etc  = new KDirMenu( parent, url, confDir, text );
         insertItem( SmallIcon( "folder_yellow" ) ,
                 i18n("&System Configuration"), m_etc);
         dirList << confDir;

         connect(m_etc , SIGNAL(fileChosen(const QString &)),
                 SLOT(slotFileChosen(const QString &) ) );
     }

     if ( url.isLocalFile()
             && dirList.find( url.path() ) == dirList.end()
             && QFileInfo( url.path() ).isWritable()
             && QFileInfo( url.path() ).isDir() 
             && kapp->authorizeURLAction("list", url, url) )
             //Need to check whether a directory so we don't crash trying to access it
             //(#60192)
     {
         // Also add current working directory
         m_current = new KDirMenu( parent, url, url.path(), text );
         insertItem( SmallIcon( "folder" ), i18n( "&Current Folder" ),
                 m_current );

         connect(m_current, SIGNAL(fileChosen(const QString &)),
                 SLOT(slotFileChosen(const QString &) ) );
     }

    if ( imProxy )
    {
        m_contacts = new KIMContactMenu( parent, imProxy );
        int item = insertItem( SmallIconSet( "personal" ), i18n( "C&ontact" ), m_contacts );
        connect ( m_contacts, SIGNAL( contactChosen( const QString &) ), SIGNAL( contactChosen( const QString & ) ) );
        if ( !imProxy->initialize() || imProxy->fileTransferContacts().isEmpty() )
          setItemEnabled( item, false );
    }


     m_browse = new KAction(i18n("&Browse..."), 0, this, SLOT(slotBrowse()), this );
     m_browse->plug(this);
     // read the last chosen dirs
     // first set the group according to our parameter
     conf = kapp->config( );
     conf->setGroup(key );
     recent_no = conf->readNumEntry("ShowRecent", 5);
     list = conf->readPathListEntry("Paths");
     if ( list.count() > 0 )
         insertSeparator();
     int i=1;
     QStringList::Iterator it = list.begin();
     while( it != list.end() ) {
         if( i == (recent_no + 1) )
	     break;
         QDir dir( *it );
         u.setPath( *it );
         if ( !dir.exists() || !kapp->authorizeURLAction("list", u, u) ) {
             it = list.remove( it );
             continue;
         }
         QString escapedDir = *it;
         KAction *action = new KAction(escapedDir.replace("&", "&&"), 0, this, SLOT(slotFastPath()), this);
         action->plug(this );
         actions.append( action );
         ++it;
         i++;
     }
}

KMetaMenu::KMetaMenu( ){

}
KMetaMenu::~KMetaMenu(){
     delete m_root;
     delete m_home;
     delete m_etc;
     delete m_current;
     delete m_browse;
     actions.clear();
}
void KMetaMenu::slotFileChosen(const QString &path ){
     writeConfig(path );
     emit fileChosen(path );
}

void KMetaMenu::slotFastPath( ) {
     KAction *action;
     action = (KAction*) sender();
     QString text = action->plainText( );
     slotFileChosen( text );
}
void KMetaMenu::writeConfig( const QString &path){
     list.remove(path );
     list.prepend(path );
     conf->setGroup( group );
     int c = conf->readNumEntry( "ShowRecent", 5 );
     while ( list.count() > c && !list.isEmpty() )
        list.remove(list.last());
     conf->writePathEntry("Paths", list);
     conf->sync( );
}
void KMetaMenu::slotBrowse() {
     KURL dest = KFileDialog::getExistingURL();
     if( dest.isEmpty() ) return;
     slotFileChosen( dest.isLocalFile() ? dest.path() : dest.url() );
}