1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef __nsNntpIncomingServer_h
7 #define __nsNntpIncomingServer_h
8 
9 #include "nsINntpIncomingServer.h"
10 #include "nsIUrlListener.h"
11 #include "nscore.h"
12 
13 #include "nsMsgIncomingServer.h"
14 
15 #include "prmem.h"
16 #include "plstr.h"
17 #include "prprf.h"
18 
19 #include "nsIMsgWindow.h"
20 #include "nsISubscribableServer.h"
21 #include "nsITimer.h"
22 #include "nsIFile.h"
23 #include "nsITreeView.h"
24 #include "nsITreeSelection.h"
25 #include "nsCOMArray.h"
26 #include "nsTArray.h"
27 
28 #include "nsNntpMockChannel.h"
29 
30 /* get some implementation from nsMsgIncomingServer */
31 class nsNntpIncomingServer : public nsMsgIncomingServer,
32                              public nsINntpIncomingServer,
33                              public nsIUrlListener,
34                              public nsISubscribableServer,
35                              public nsITreeView
36 
37 {
38  public:
39   NS_DECL_ISUPPORTS_INHERITED
40   NS_DECL_NSINNTPINCOMINGSERVER
41   NS_DECL_NSIURLLISTENER
42   NS_DECL_NSISUBSCRIBABLESERVER
43   NS_DECL_NSITREEVIEW
44 
45   nsNntpIncomingServer();
46 
47   NS_IMETHOD GetLocalStoreType(nsACString& type) override;
48   NS_IMETHOD GetLocalDatabaseType(nsACString& type) override;
49   NS_IMETHOD CloseCachedConnections() override;
50   NS_IMETHOD PerformBiff(nsIMsgWindow* aMsgWindow) override;
51   NS_IMETHOD PerformExpand(nsIMsgWindow* aMsgWindow) override;
52   NS_IMETHOD OnUserOrHostNameChanged(const nsACString& oldName,
53                                      const nsACString& newName,
54                                      bool hostnameChanged) override;
55 
56   // for nsMsgLineBuffer
57   virtual nsresult HandleLine(const char* line, uint32_t line_size);
58 
59   // override to clear all passwords associated with server
60   NS_IMETHODIMP ForgetPassword() override;
61   NS_IMETHOD GetCanCompactFoldersOnServer(
62       bool* canCompactFoldersOnServer) override;
63   NS_IMETHOD GetCanSearchMessages(bool* canSearchMessages) override;
64   NS_IMETHOD GetOfflineSupportLevel(int32_t* aSupportLevel) override;
65   NS_IMETHOD GetDefaultCopiesAndFoldersPrefsToServer(
66       bool* aCopiesAndFoldersOnServer) override;
67   NS_IMETHOD GetCanCreateFoldersOnServer(
68       bool* aCanCreateFoldersOnServer) override;
69   NS_IMETHOD GetCanFileMessagesOnServer(
70       bool* aCanFileMessagesOnServer) override;
71   NS_IMETHOD GetFilterScope(nsMsgSearchScopeValue* filterScope) override;
72   NS_IMETHOD GetSearchScope(nsMsgSearchScopeValue* searchScope) override;
73 
74   NS_IMETHOD GetSocketType(
75       int32_t* aSocketType) override;  // override nsMsgIncomingServer impl
76   NS_IMETHOD SetSocketType(
77       int32_t aSocketType) override;  // override nsMsgIncomingServer impl
78   NS_IMETHOD GetSortOrder(int32_t* aSortOrder) override;
79 
80  protected:
81   virtual ~nsNntpIncomingServer();
82   virtual nsresult CreateRootFolderFromUri(const nsACString& serverUri,
83                                            nsIMsgFolder** rootFolder) override;
84   nsresult GetNntpConnection(nsIURI* url, nsIMsgWindow* window,
85                              nsINNTPProtocol** aNntpConnection);
86   nsresult CreateProtocolInstance(nsINNTPProtocol** aNntpConnection,
87                                   nsIURI* url, nsIMsgWindow* window);
88   bool ConnectionTimeOut(nsINNTPProtocol* aNntpConnection);
89 
90   // The pool of current connections (up to 'max_cached_connections').
91   nsCOMArray<nsINNTPProtocol> mConnectionCache;
92 
93   // Pending requests which couldn't immediately be handled by a connection.
94   nsTArray<RefPtr<nsNntpMockChannel> > m_queuedChannels;
95 
96   /**
97    * Downloads the newsgroup headers.
98    */
99   nsresult DownloadMail(nsIMsgWindow* aMsgWindow);
100 
101   NS_IMETHOD GetServerRequiresPasswordForBiff(
102       bool* aServerRequiresPasswordForBiff) override;
103   nsresult SetupNewsrcSaveTimer();
104   static void OnNewsrcSaveTimer(nsITimer* timer, void* voidIncomingServer);
105   void WriteLine(nsIOutputStream* stream, nsCString& str);
106 
107  private:
108   nsTArray<nsCString> mSubscribedNewsgroups;
109   nsTArray<nsCString> mGroupsOnServer;
110   nsTArray<nsCString> mSubscribeSearchResult;
111   bool mSearchResultSortDescending;
112   // the list of of subscribed newsgroups within a given
113   // subscribed dialog session.
114   // we need to keep track of them so we know what to show as "checked"
115   // in the search view
116   nsTArray<nsCString> mTempSubscribed;
117 
118   RefPtr<mozilla::dom::XULTreeElement> mTree;
119   nsCOMPtr<nsITreeSelection> mTreeSelection;
120 
121   bool mHasSeenBeginGroups;
122   bool mGetOnlyNew;
123   nsresult WriteHostInfoFile();
124   nsresult LoadHostInfoFile();
125   nsresult AddGroupOnServer(const nsACString& name);
126 
127   bool mNewsrcHasChanged;
128   bool mHostInfoLoaded;
129   bool mHostInfoHasChanged;
130   nsCOMPtr<nsIFile> mHostInfoFile;
131 
132   uint32_t mLastGroupDate;
133   int32_t mUniqueId;
134   uint32_t mLastUpdatedTime;
135   int32_t mVersion;
136   bool mPostingAllowed;
137 
138   nsCOMPtr<nsITimer> mNewsrcSaveTimer;
139   nsCOMPtr<nsIMsgWindow> mMsgWindow;
140 
141   nsCOMPtr<nsISubscribableServer> mInner;
142   nsresult EnsureInner();
143   nsresult ClearInner();
144   bool IsValidRow(int32_t row);
145   nsCOMPtr<nsIFile> mNewsrcFilePath;
146 };
147 
148 #endif
149