summaryrefslogtreecommitdiffstats
path: root/ark/arkapp.cpp
blob: 269b1553aa5627deeb56d843d91f3e76e470243d (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
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
/*

 ark -- archiver for the KDE project

 Copyright (C)

 2002-2003: Helio Chissini de Castro <helio@conectiva.com.br>
 2003: Georg Robbers <Georg.Robbers@urz.uni-hd.de>
 1999-2000: Corel Corporation (author: Emily Ezust  emilye@corel.com)
 1999: Francois-Xavier Duranceau duranceau@kde.org

 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.

*/

#include <dcopclient.h>
#include <kdebug.h>
#include <tdecmdlineargs.h>
#include <klocale.h>
#include <unistd.h>
#include <tqfile.h>
#include <errno.h>


#include "arkapp.h"

ArkApplication *ArkApplication::mInstance = NULL;

// a helper function to follow a symlink and obtain the real filename
// Used in the ArkApplication functions that use the archive filename
// to make sure an archive isn't opened twice in different windows
// Now, readlink only gives one level so this function recurses.

static TQString resolveFilename(const TQString & _arkname)
{
	char *buff;
	int nread;
	int iter = 1;
	
	while ( true )
	{
		buff = new char[BUFSIZ*iter];
		nread = readlink( TQFile::encodeName(_arkname), buff, BUFSIZ);
		if (-1 == nread)
		{
			if ( EINVAL == errno )  // not a symbolic link. Stopping condition.
			{
				delete [] buff;
				return _arkname;
			}
			else if ( ENAMETOOLONG == errno )
			{
				kdDebug(1601) << "resolveFilename: have to reallocate - name too long!" << endl;
				iter++;
				delete [] buff;
				continue;
			}
			else
			{
				delete [] buff;
				// the other errors will be taken care of already in simply
				// // opening the archive (i.e., the user will be notified)
				return "";
			}
		}
      else
		{
			buff[nread] = '\0';  // readlink doesn't null terminate
			TQString name = TQFile::decodeName( buff );
			delete [] buff;
			
			// watch out for relative pathnames
			if (name.at(0) != '/')
			{
				// copy the path from _arkname
				int index = _arkname.findRev('/');
				name = _arkname.left(index + 1) + name;
			}
			kdDebug(1601) << "Now resolve " << name << endl;
			
			return resolveFilename( name );
		}
	}
}


ArkApplication * ArkApplication::getInstance()
{
	if (mInstance == NULL)
	{
		mInstance = new ArkApplication();
	}
	return mInstance;
}

ArkApplication::ArkApplication()
  : KUniqueApplication(), m_windowCount(0)
{
	m_mainwidget = new TQWidget;
	setMainWidget(m_mainwidget);
}

int
ArkApplication::newInstance()
{

    // If we are restored by session management, we don't need to open
    // another window on startup.
    if (restoringSession()) return 0;

    TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

    if ( args->isSet( "extract-to" ) )
    {
        if ( args->count() == 2 )
        {
            MainWindow *arkWin = new MainWindow();

            arkWin->extractTo( args->url( 0 ), args->url( 1 ), args->isSet( "guess-name" ) );
            return 0;
        }
        else
        {
            TDECmdLineArgs::usage( i18n( "Wrong number of arguments specified" ) );
            return 0;
        }
    }

    if ( args->isSet( "add-to" ) && ( !args->isSet( "add" ) ) )
    {
        if ( args->count() < 2 )
        {
            TDECmdLineArgs::usage( i18n( "You need to specify at least one file to be added to the archive." ) );
            return 0;
        }
        else
        {
            KURL::List URLList;
            for ( int c = 0; c < args->count()-1 ; c++ )
                URLList.append( args->url( c ) );

            MainWindow *arkWin = new MainWindow();

            arkWin->addToArchive( URLList, args->cwd(), args->url( args->count()-1 ) );
            return 0;
        }
    }

    if ( args->isSet( "add" ) && args->isSet( "add-to" ) )   // HACK
    {
        bool oneFile = (args->count() == 2 ) ;

        TQString extension = args->arg( 0 );
        KURL archiveName = args->url( 1 );  // the filename

        // if more than one file -> use directory name
        if ( !oneFile )
            archiveName.setPath( archiveName.directory() );

        archiveName.setFileName( archiveName.fileName() + extension );
        KURL::List URLList;
        for ( int c = 1; c < args->count(); c++ )
            URLList.append( args->url( c ) );

        MainWindow *arkWin = new MainWindow();

        arkWin->addToArchive( URLList, args->cwd(), archiveName, !oneFile );
        return 0;
    }


    if ( args->isSet( "add" ) && ( !args->isSet( "add-to" ) ) )
    {
        if ( args->count() < 1 )
        {
            TDECmdLineArgs::usage( i18n( "You need to specify at least one file to be added to the archive." ) );
            return 0;
        }
        else
        {
            KURL::List URLList;
            for ( int c = 0; c < args->count() ; c++ )
                URLList.append( args->url( c ) );

            MainWindow *arkWin = new MainWindow();

            arkWin->addToArchive( URLList, args->cwd() );
            return 0;
        }
    }


    int i = 0;
    KURL url;
    bool doAutoExtract = args->isSet("extract");
    bool tempFile = TDECmdLineArgs::isTempFileSet();
    do
    {
        if (args->count() > 0)
        {
            url = args->url(i);
        }
        MainWindow *arkWin = new MainWindow();
        arkWin->show();
        if(doAutoExtract)
        {
            arkWin->setExtractOnly(true);
        }
        if (!url.isEmpty())
        {
            arkWin->openURL(url, tempFile);
        }

        ++i;
    } while  (i < args->count());

    args->clear();
    return 0;
}


void
ArkApplication::addOpenArk(const KURL & _arkname, MainWindow *_ptr)
{
    TQString realName;
    if( _arkname.isLocalFile() )
    {
        realName = resolveFilename( _arkname.path() );  // follow symlink
        kdDebug(1601) << " Real name of " << _arkname.prettyURL() << " is " << realName << endl;
    }
    else
        realName = _arkname.prettyURL();
    openArksList.append(realName);
    m_windowsHash.replace(realName, _ptr);
    kdDebug(1601) << "Saved ptr " << _ptr << " added open ark: " << realName << endl;
}

void
ArkApplication::removeOpenArk(const KURL & _arkname)
{
    TQString realName;
    if ( _arkname.isLocalFile() )
        realName = resolveFilename( _arkname.path() );  // follow symlink
    else
        realName = _arkname.prettyURL();
    kdDebug(1601) << "Removing name " << _arkname.prettyURL() << endl;
    openArksList.remove(realName);
    m_windowsHash.remove(realName);
}

void
ArkApplication::raiseArk(const KURL & _arkname)
{
    kdDebug( 1601 ) << "ArkApplication::raiseArk " << endl;
    MainWindow *window;
    TQString realName;
    if( _arkname.isLocalFile() )
        realName = resolveFilename(_arkname.path());  // follow symlink
    else
        realName = _arkname.prettyURL();
    window = m_windowsHash[realName];
    kdDebug(1601) << "ArkApplication::raiseArk " << window << endl;
    // raise didn't seem to be enough. Not sure why!
    // This might be annoying though.
    //window->hide();
    //window->show();
    window->raise();
}

bool
ArkApplication::isArkOpenAlready(const KURL & _arkname)
{
    TQString realName;
    if ( _arkname.isLocalFile() )
        realName = resolveFilename(_arkname.path());  // follow symlink
    else
        realName = _arkname.prettyURL();
	return ( openArksList.findIndex(realName) != -1 );
}

#include "arkapp.moc"