summaryrefslogtreecommitdiffstats
path: root/src/parsers/policy.cpp
blob: af099927d54d45060d1ba228e6646764d5c4459f (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
/***************************************************************************
 *   Copyright (C) 2003 by Sylvain Joyeux                                  *
 *   sylvain.joyeux@m4x.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.                                   *
 ***************************************************************************/

#include "parsers.h"
#include "../apt.h"

#include <tdelocale.h>
#include <kdebug.h>
#include <qhtmlstream.h>
#include <tqregexp.h>

static void	policy_begin(TQHtmlStream& stream)
{
  stream
    << block("div") << endl
    << block("table", "policy") << endl
    	<< block("tr") << block("td") << endl
    		<< block("table", "curver") << endl
    			<< block("tbody");
}

static void add_button(TQHtmlStream& stream, const TQString& mode, const TQString& text, const TQString& package)
{
  stream
    << block("form")
        << param("action") << "apt:/"
        << param("method") << "get" << endl
      << block("p") << endl
        << tag("input")
          << param("type") << "hidden"
          << param("name") << "get"
          << param("value") << mode << endl
        << tag("input")
          << param("type") << "hidden"
          << param("name") << "package"
          << param("value") << package << endl
        << tag("input")
          << param("type") << "submit"
          << param("value") << text << endl
      << close()
    << close() << endl;
}

static void add_version_link(TQHtmlStream& stream, AptProtocol* slave, const TQString& package, const TQString& version)
{
  KURL url(slave->buildURL("show", package));
  url.setHTMLRef(Parsers::mangle_version(version));

  stream
    << block("a", "vtable-version")
        << param("href") << url.htmlURL()
      << data() << version
    << close();
}

namespace Parsers
{

Policy::Policy(const TQString& package, bool act)
  : m_package(package), m_act(false)
{
  m_act = act;
}

void Policy::operator() (AptProtocol* slave, const TQString& type, const TQString& value)
{
  static bool first_version = false, received_sth = false;
  static TQString buffer;
  static TQHtmlStream* stream;

  static TQRegExp rx_notinstalled("(none)");

  kdDebug(7130) << "policy : " << type << " " << value << endl;

  if (type == "begin")
  {
    stream = new TQHtmlStream(&buffer);
  	policy_begin(*stream);
  }
  else if (type == "installed")
  {
  	received_sth = true;

    attribute_begin(*stream, i18n("Installed"));
    if (rx_notinstalled.search(value) >= 0)
    {
    	m_installed = TQString();
      *stream << i18n("no");
    }
    else
  	{
    	m_installed = value.stripWhiteSpace();
      add_version_link(*stream, slave, m_package, m_installed);
  	}
    attribute_end(*stream);
 }
  else if (type == "candidate")
  {
  	received_sth = true;

    bool has_candidate = (rx_notinstalled.search(value) == -1);

    if (m_act && has_candidate)
    {
      *stream
        << block("tr") << endl
          << block("td") << param("colspan") << 2 << endl;

      if (m_installed.isEmpty())
        add_button(*stream, "install", i18n("Install"), m_package);
      else
        add_button(*stream, "remove", i18n("Remove"), m_package);
      *stream << close() << close() << endl;
    }

    attribute_begin(*stream, i18n("Candidate"));
    if (has_candidate)
      add_version_link(*stream, slave, m_package, value);
    else
      *stream << i18n("none");
    attribute_end(*stream);

    if (m_act && has_candidate
      && !m_installed.isEmpty() && m_installed != value)
    {
      *stream
        << block("tr") << endl
          << block("td") << param("colspan") << 2 << endl;
      add_button(*stream, "install", i18n("Upgrade"), m_package);
      *stream << close() << close() << endl;
    }
  }
  else if (type == "version_table")
  {
  	received_sth = true;

    *stream
    	<< close() << close() << endl // tbody, table, form
      << close() << endl; // td

  	first_version = true;
  }
  else if (type == "version")
  {
    TQString version = value.section(' ', 0, 0);
    TQString pin = value.section(' ', 1, 1);

    if (first_version)
    {
      *stream
        << block("td") << endl
          << block("div", "vtable") << endl
            << block("div", "header") << i18n("Version Table") << close() << endl
            << block("div", "versions") << endl;
    } /*else {
      *stream << close() << close();
    }*/

    TQString version_link;
    version_link = "<a href=\"apt:/show?" + m_package + "#" + mangle_version(version) + "\">"
       + version + "</a>";

    *stream << tag("br") << endl;
    add_version_link(*stream, slave, m_package, version);
    *stream << "[Pin " << block("span", "version-pin") << pin << close() << "]";

    first_version = false;
  }
  else if (type == "location")
  {
    TQStringList sections = TQStringList::split(' ', value);
    TQString pin = sections.first();
    sections.pop_front();
    // remove the "Packages" field if it is here
    if (sections.last() == "Packages")
      sections.pop_back();

    *stream << tag("br") << endl;
    *stream << block("span", "location-pin") << pin << close() << sections.join(" ") << endl;
  }
  else if (type == "end")
  {
  	if (received_sth)
    {
      *stream << close_all() << endl;
      *slave << buffer;
   	}

    buffer = TQString();
    received_sth = false;
    delete stream;
  }
}

}