summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/sync/sinks/gallery/galleryform.cpp
blob: e726a3b28cf713ac2c3ceb5c8821a839290078bd (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
/* ============================================================
 * File  : gallerympform.cpp
 * Author: Renchi Raju <renchi@pooh.tam.uiuc.edu>
 * Date  : 2004-12-02
 * Description :
 *
 * Copyright 2004 by Renchi Raju <renchi@pooh.tam.uiuc.edu>

 * 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, 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.
 *
 * ============================================================ */

#include <kapplication.h>
#include <kdebug.h>
#include <kmimetype.h>
#include <kurl.h>

#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqtextstream.h>

//#include <cstring>
//#include <cstdio>

#include "galleryform.h"

namespace KIPISyncPlugin
{

GalleryForm::GalleryForm(GalleryVersion version, TQString authToken)
 : mVersion(version)
{
  mBoundary  = "----------";
  mBoundary += TDEApplication::randomString( 42 + 13 ).ascii();

  if (Gallery2 == mVersion)
  {
    addPairRaw("g2_controller", "remote:GalleryRemote");
    if (!authToken.isEmpty())
      addPairRaw("g2_authToken", authToken);
  }
}


GalleryForm::~GalleryForm()
{
}


void GalleryForm::addPair(const TQString& name, const TQString& value)
{
  if (Gallery2 == mVersion)
    return addPairRaw(TQString("g2_form[%1]").arg(name), value);

  return addPairRaw(name, value);
}


void GalleryForm::addPairRaw(const TQString& name, const TQString& value)
{
  TQTextStream ts(m_buffer, IO_Append|IO_WriteOnly);
  ts.setEncoding(TQTextStream::UnicodeUTF8);
  ts << "--" << mBoundary << "\r\n";
  ts << "Content-Disposition: form-data; name=\"" << name.ascii() << "\"\r\n\r\n";
  ts << value.ascii() << "\r\n";
}

bool GalleryForm::addFile(const TQString& path, const TQString& displayFilename)
{
  TQString filename = "userfile_name";
  if (Gallery2 == mVersion)
    filename = "g2_userfile_name";

  addPairRaw(filename, displayFilename);
  KMimeType::Ptr ptr = KMimeType::findByURL(path);
  TQString mime = ptr->name();
  if (mime.isEmpty())
  {
    // if we ourselves can't determine the mime of the local file,
    // very unlikely the remote gallery will be able to identify it
    return false;
  }

  TQFile imageFile(path);
  if ( !imageFile.open( IO_ReadOnly ) )
    return false;
  TQByteArray imageData = imageFile.readAll();
  imageFile.close();

  TQTextStream ts(m_buffer, IO_Append|IO_WriteOnly);
  ts.setEncoding(TQTextStream::UnicodeUTF8);
  ts << "--" << mBoundary << "\r\n";
  ts << "Content-Disposition: form-data; name=\"";
  if (Gallery2 == mVersion)
    ts << "g2_userfile";
  else
    ts << "userfile";
  ts << "\"; filename=\"" << TQFile::encodeName(KURL(path).filename()) << "\"\r\n";
  ts << "Content-Type: " << mime.ascii() << "\r\n\r\n";

  int oldSize = m_buffer.size();
  m_buffer.resize(oldSize + imageData.size() + 3);
  memcpy(m_buffer.data()+oldSize, imageData.data(), imageData.size());
  m_buffer[m_buffer.size()-3] = '\r';
  m_buffer[m_buffer.size()-2] = '\n';
  m_buffer[m_buffer.size()-1] = '\0';
  
  return true;
}


TQString GalleryForm::contentType() const
{
  return TQString("Content-Type: multipart/form-data; boundary=" + mBoundary);
}


TQByteArray GalleryForm::formData()
{
  TQTextStream ts(m_buffer, IO_Append|IO_WriteOnly);
  ts.setEncoding(TQTextStream::UnicodeUTF8);
  ts << "--" << mBoundary << "--" << "\r\n";

  return m_buffer;
}

}