summaryrefslogtreecommitdiffstats
path: root/superkaramba/src/sknewstuff.cpp
blob: bc8f7d404b124f08b9716040b43ea6dceb2a6cda (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
/*
 * Copyright (C) 2005 Ryan Nickell <p0z3r @ earthlink . net>
 *
 * This file is part of SuperKaramba.
 *
 *  SuperKaramba 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.
 *
 *  SuperKaramba 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 SuperKaramba; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 ****************************************************************************/

#include <tdeapplication.h>
#include <kdebug.h>
#include <tdefilemetainfo.h>
#include <tdeio/netaccess.h>
#include <kmimetype.h>
#include <krun.h>
#include <kstandarddirs.h>
#include <ktar.h>
#include <kurl.h>
#include <tqdir.h>
#include <tqfileinfo.h>

#ifdef HAVE_CONFIG_H
  #include <config.h>
#endif

#include "karambaapp.h"
#include "themesdlg.h"
#ifdef HAVE_KNEWSTUFF
#include "sknewstuff.h"

SKNewStuff::SKNewStuff( ThemesDlg *dlg ) :
  TDENewStuff( "superkaramba/themes", dlg ),
  mDlg( dlg )
{
}

bool SKNewStuff::install( const TQString &fileName )
{
  kdDebug() << "SKNewStuff::install(): " << fileName << endl;

  KMimeType::Ptr result = KMimeType::findByURL(fileName);
  TDEStandardDirs myStdDir;
  TQFileInfo fi(fileName);
  TQString base = fi.baseName();
  TQString baseDestDir =myStdDir.saveLocation("data", kapp->instanceName() + "/themes/", true);
  const TQString destDir = baseDestDir + base + "/";
  TDEStandardDirs::makeDir( destDir );

  kdDebug() << "SKNewStuff::install() mimetype: " << result->name() << endl;

  if( result->name() == "application/x-gzip" ||
      result->name() == "application/x-tgz" ||
      result->name() == "application/x-bzip" ||
      result->name() == "application/x-bzip2" ||
      result->name() == "application/x-tbz" ||
      result->name() == "application/x-tbz2" ||
      result->name() == "application/x-tar" ||
      result->name() == "application/x-tarz")
  {
    kdDebug() << "SKNewStuff::install() gzip/bzip2 mimetype encountered" <<
        endl;
    KTar archive( fileName );
    if ( !archive.open( IO_ReadOnly ) )
      return false;
    const KArchiveDirectory *archiveDir = archive.directory();
    archiveDir->copyTo(destDir);
    //Add the theme to the Theme Dialog
    mDlg->addThemeToDialog(archiveDir, destDir);
    archive.close();
  }
  else if(result->name() == "application/x-zip" ||
          result->name() == "application/x-superkaramba")
  {
    kdDebug() << "SKNewStuff::install() zip mimetype encountered" << endl;
    //TODO: write a routine to check if this is a valid .skz file
    //otherwise we need to unpack it like it is an old theme that was packaged
    //as a .zip instead of .bz2 or .tar.gz
    KURL sourceFile(fileName);
    KURL destFile( destDir + sourceFile.fileName() );
    if(!TDEIO::NetAccess::file_copy( sourceFile, destFile ))
    {
      return false;
    }
    TDEIO::NetAccess::removeTempFile( sourceFile.url() );
    //Add the skz theme to the Theme Dialog
    mDlg->addSkzThemeToDialog(destFile.path());
  }
  else if(result->name() == "plain/text")
  {
    kdDebug() << "SKNewStuff::install() plain text" << endl;
  }
  else if(result->name() == "text/html")
  {
    kdDebug() << "SKNewStuff::install() text/html" << endl;
    KRun::runURL( m_sourceLink, "text/html");
  }
  else
  {
    kdDebug() << "SKNewStuff::install() Error no compatible mimetype encountered to install"
              << endl;
    return false;
  }
  return true;
}

bool SKNewStuff::createUploadFile( const TQString &fileName )
{
  kdDebug() << "SKNewStuff::createUploadFile(): " << fileName << endl;
  return true;
}

TQString SKNewStuff::downloadDestination( KNS::Entry *entry )
{
  KURL source = entry->payload();
  m_sourceLink = source;

  kdDebug() << "SKNewStuff::downloadDestination() url: "
    << source.url() <<  " fileName: " << source.fileName() << endl;
  TQString file(source.fileName());
  if ( file.isEmpty() )
  {
    kdDebug() << "The file was empty. " << source.url() << 
      " must be a URL link." << endl;
    KRun::runURL( source, "text/html");
    return file;
  }
  return TDEGlobal::dirs()->saveLocation( "tmp" ) + source.fileName();
}
#endif //HAVE_KNEWSTUFF