1 /*
2  * lftp - file transfer program
3  *
4  * Copyright (c) 1996-2012 by Alexander V. Lukyanov (lav@yars.free.net)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <config.h>
21 #include "FileAccess.h"
22 #include "ConnectionSlot.h"
23 
24 ConnectionSlot ConnectionSlot::lftp_slots;
25 
SlotValue(const char * n,const FileAccess * s)26 ConnectionSlot::SlotValue::SlotValue(const char *n,const FileAccess *s)
27    : KeyValueDB::Pair(n,s->GetConnectURL())
28 {
29    session=s->Clone();
30 }
SlotValue(const char * n,const char * v)31 ConnectionSlot::SlotValue::SlotValue(const char *n,const char *v)
32    : KeyValueDB::Pair(n,v)
33 {
34    session=FileAccess::New(v);
35 }
Find(const char * n)36 ConnectionSlot::SlotValue *ConnectionSlot::Find(const char *n)
37 {
38    KeyValueDB::Pair **slot=lftp_slots.LookupPair(n);
39    return slot?static_cast<SlotValue*>(*slot):0;
40 }
Set(const char * n,const FileAccess * fa)41 void ConnectionSlot::Set(const char *n,const FileAccess *fa)
42 {
43    const char *url=fa->GetConnectURL();
44    if(!url || !*url)
45    {
46       lftp_slots.KeyValueDB::Remove(n);
47       return;
48    }
49    ConnectionSlot::SlotValue *s=Find(n);
50    if(!s)
51    {
52       lftp_slots.AddPair(new SlotValue(n,fa));
53       return;
54    }
55    if(!s->session->SameLocationAs(fa))
56    {
57       s->SetValue(url);
58       s->session=fa->Clone();
59    }
60 }
SetCwd(const char * n,const FileAccess::Path & cwd)61 void ConnectionSlot::SetCwd(const char *n,const FileAccess::Path &cwd)
62 {
63    ConnectionSlot::SlotValue *s=Find(n);
64    if(!s || !s->session)
65       return;
66    s->session->SetCwd(cwd);
67    s->SetValue(s->session->GetConnectURL());
68 }
FindSession(const char * n)69 const FileAccess *ConnectionSlot::FindSession(const char *n)
70 {
71    ConnectionSlot::SlotValue *s=Find(n);
72    if(s)
73       return s->session;
74    return 0;
75 }
Format()76 char *ConnectionSlot::Format()
77 {
78    return lftp_slots.FormatThis();
79 }
FormatThis()80 char *ConnectionSlot::FormatThis()
81 {
82    return KeyValueDB::Format();
83 }
Cleanup()84 void ConnectionSlot::Cleanup()
85 {
86    lftp_slots.Empty();
87 }
ConnectionSlot()88 ConnectionSlot::ConnectionSlot() : KeyValueDB() {}
~ConnectionSlot()89 ConnectionSlot::~ConnectionSlot() {}
90