summaryrefslogtreecommitdiffstats
path: root/src/fetch/fetcher.h
blob: ef519dc7f18916f3a43ab194fd1a21c2f529c8c4 (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
/***************************************************************************
    copyright            : (C) 2003-2006 by Robby Stephenson
    email                : robby@periapsis.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#ifndef FETCHER_H
#define FETCHER_H

#include "fetch.h"
#include "../datavectors.h"

#include <tdeapplication.h> // for TDEApplication::random()

#include <tqobject.h>
#include <tqstring.h>

class TDEConfigGroup;

namespace Tellico {
  namespace Fetch {
    class ConfigWidget;
    class MessageHandler;
    class SearchResult;

/**
 * The top-level abstract class for fetching data.
 *
 * @author Robby Stephenson
 */
class Fetcher : public TQObject, public TDEShared {
Q_OBJECT
  

public:
  typedef TDESharedPtr<Fetcher> Ptr;
  typedef TDESharedPtr<const Fetcher> CPtr;

  /**
   */
  Fetcher(TQObject* parent, const char* name = 0) : TQObject(parent, name), TDEShared(),
                                                   m_updateOverwrite(false), m_hasMoreResults(false),
                                                   m_messager(0) {}
  /**
   */
  virtual ~Fetcher();

  /**
   * Returns true if the fetcher might return entries from a certain collection type.
   */
  virtual bool canFetch(int type) const = 0;
  /**
   * Returns true if the fetcher can search using a certain key.
   */
  virtual bool canSearch(FetchKey key) const = 0;
  virtual bool canUpdate() const { return true; }

  /**
   * Returns the type of the data source.
   */
  virtual Type type() const = 0;
  /**
   * Returns the name of the data source, as defined by the user.
   */
  virtual TQString source() const = 0;
  /**
   * Returns whether the fetcher will overwite existing info when updating
   */
  bool updateOverwrite() const { return m_updateOverwrite; }
  /**
   * Starts a search, using a key and value.
   */
  virtual void search(FetchKey key, const TQString& value) = 0;
  virtual void continueSearch() {}
  virtual void updateEntry(Data::EntryPtr);
  // mopst fetchers won't support this. it's particular useful for text fetchers
  virtual void updateEntrySynchronous(Data::EntryPtr) {}
  /**
   * Returns true if the fetcher is currently searching.
   */
  virtual bool isSearching() const = 0;
  /**
   * Returns true if the fetcher can continue and fetch more results
   * The fetcher is responsible for remembering state.
   */
  virtual bool hasMoreResults() const { return m_hasMoreResults; }
  /**
   * Stops the fetcher.
   */
  virtual void stop() = 0;
  /**
   * Fetches an entry, given the uid of the search result.
   */
  virtual Data::EntryPtr fetchEntry(uint uid) = 0;

  void setMessageHandler(MessageHandler* handler) { m_messager = handler; }
  MessageHandler* messageHandler() const { return m_messager; }
  /**
   */
  void message(const TQString& message, int type) const;
  void infoList(const TQString& message, const TQStringList& list) const;

  /**
   * Reads the config for the widget, given a config group.
   */
  void readConfig(const TDEConfigGroup& config, const TQString& groupName);
  /**
   * Returns a widget for modifying the fetcher's config.
   */
  virtual ConfigWidget* configWidget(TQWidget* parent) const = 0;

signals:
//  void signalStatus(const TQString& status);
  void signalResultFound(Tellico::Fetch::SearchResult* result);
  void signalDone(Tellico::Fetch::Fetcher::Ptr);

protected:
  TQString m_name;
  bool m_updateOverwrite : 1;
  bool m_hasMoreResults : 1;

private:
  virtual void readConfigHook(const TDEConfigGroup&) = 0;
  virtual void saveConfigHook(TDEConfigGroup&) {}

  MessageHandler* m_messager;
  TQString m_configGroup;
};

class SearchResult {
public:
  SearchResult(Fetcher::Ptr f, const TQString& t, const TQString& d, const TQString& i)
   : uid(TDEApplication::random()), fetcher(f), title(t), desc(d), isbn(i) {}
  Data::EntryPtr fetchEntry();
  uint uid;
  Fetcher::Ptr fetcher;
  TQString title;
  TQString desc;
  TQString isbn;
};

  } // end namespace
} // end namespace

#endif