summaryrefslogtreecommitdiffstats
path: root/src/app.cpp
blob: 99f570be54a50b4aeac334fcca3440667d8ef2f5 (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
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
/***************************************************************************
 * $Id: tork.cpp,v 1.160 2007/12/30 12:58:22 hoganrobert Exp $
 *   Copyright (C) 2006 by Robert Hogan                                    *
 *   robert@roberthogan.net                                                *
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.              *
 ***************************************************************************/

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
#endif


#include <tqcstring.h>
#include <tqsocket.h>
#include <tqdatetime.h>
#include <tqbitarray.h>

#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#include <kapplication.h>
#include <kdebug.h>
#include <kmessagebox.h>
#include <kinstance.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kurl.h>
#include <ksock.h>

#include "app.h"

using namespace KIO;

extern "C"
{
    int kdemain(int argc, char **argv)
    {
        KInstance instance( "kio_app" );
        
        kdDebug(7101) << "*** Starting kio_app " << endl;
        
        if (argc != 4) {
            kdDebug(7101) << "Usage: kio_app  protocol domain-socket1 domain-socket2" << endl;
            exit(-1);
        }
        
        kio_appProtocol slave(argv[2], argv[3]);
        slave.dispatchLoop();
        
        kdDebug(7101) << "*** kio_app Done" << endl;
        return 0;
    }
} 


kio_appProtocol::kio_appProtocol(const TQCString &pool_socket, const TQCString &app_socket)
    : SlaveBase("kio_app", pool_socket, app_socket)
{
    kdDebug() << "kio_appProtocol::kio_appProtocol()" << endl;
}


kio_appProtocol::~kio_appProtocol()
{
    kdDebug() << "kio_appProtocol::~kio_appProtocol()" << endl;
}


void kio_appProtocol::stat(const KURL &url)
{
	kdDebug() << "kio_appProtocol::stat: " << url << endl;

	TQString path = url.path();
	if ( path.isEmpty() || path == "/" )
	{
    	kdDebug() << "kio_appProtocol::stat: " << "creating top level entry" << endl;
		// The root is "virtual" - it's not a single physical directory
		KIO::UDSEntry entry;
		m_impl.createTopLevelEntry( entry );
		statEntry( entry );
		finished();
		return;
	}

	TQString name;
	bool ok = m_impl.parseURL(url, name, path);

	if ( !ok )
	{
    	kdDebug() << "kio_appProtocol::stat: " << "can't parse url" << endl;
		error(KIO::ERR_MALFORMED_URL, url.prettyURL());
		return;
	}


	if( path.isEmpty() )
	{
    	kdDebug() << "kio_appProtocol::stat4: " << "url empty after parsing" << endl;

		KIO::UDSEntry entry;

		if ( m_impl.statByName(name, entry) )
		{
			statEntry(entry);
			finished();
		}
		else
		{
			error(KIO::ERR_DOES_NOT_EXIST, url.prettyURL());
		}
	}
	else
	{
    	kdDebug() << "kio_appProtocol::stat4: " << "url not empty after parsing: statting" << endl;
		SlaveBase::stat(url);
	}
}

void kio_appProtocol::listDir(const KURL &url)
{
	kdDebug() << "kio_appProtocol::listDir: " << url << endl;

	if ( url.path().length() <= 1 )
	{
    	kdDebug() << "kio_appProtocol::listDir: " << "url empty: listing root" << endl;
		listRoot();
		return;
	}

	TQString name, path;
	bool ok = m_impl.parseURL(url, name, path);

	if ( !ok )
	{
		error(KIO::ERR_MALFORMED_URL, url.prettyURL());
		return;
	}

	kdDebug() << "kio_appProtocol::listDir: " << "url is " << url << ": doing a listDir" << endl;
	kdDebug() << "kio_appProtocol::listDir: " << "name is " << name << ": doing a listDir" << endl;
    kdDebug() << "kio_appProtocol::listDir: " << "path is " << path << ": doing a listDir" << endl;
    // We've been given something like app:/appname
	listAppContents(name);
}

void kio_appProtocol::listRoot()
{
	KIO::UDSEntry entry;

	KIO::UDSEntryList system_entries;
	bool ok = m_impl.listRoot(system_entries);

	if (!ok)
	{
		error( m_impl.lastErrorCode(), m_impl.lastErrorMessage() );
		return;
	}

	totalSize(system_entries.count()+1);

	m_impl.createTopLevelEntry(entry);
	listEntry(entry, false);

	KIO::UDSEntryListIterator it = system_entries.begin();
	KIO::UDSEntryListIterator end = system_entries.end();

	for(; it!=end; ++it)
	{
		listEntry(*it, false);
	}

	entry.clear();
	listEntry(entry, true);

	finished();
}


void kio_appProtocol::listAppContents(const TQString &name)
{
	KIO::UDSEntry entry;

	KIO::UDSEntryList app_entries;
	bool ok = m_impl.listAppContents(name, app_entries);

	if (!ok)
	{
		error( m_impl.lastErrorCode(), m_impl.lastErrorMessage() );
		return;
	}

	totalSize(app_entries.count()+1);

	m_impl.createTopLevelEntry(entry);
	listEntry(entry, false);

	KIO::UDSEntryListIterator it = app_entries.begin();
	KIO::UDSEntryListIterator end = app_entries.end();

	for(; it!=end; ++it)
	{
		listEntry(*it, false);
	}

	entry.clear();
	listEntry(entry, true);

	finished();

}