summaryrefslogtreecommitdiffstats
path: root/src/cite/lyxpipe.cpp
blob: 4943a8a7032d303ce30401abc0f408444cc1d569 (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
/***************************************************************************
    copyright            : (C) 2005-2006 by Robby Stephenson
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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;                            *
 *                                                                         *
 ***************************************************************************/

#include "lyxpipe.h"
#include "../collection.h"
#include "../translators/bibtexhandler.h"
#include "../tellico_kernel.h"
#include "../core/tellico_config.h"
#include "../tellico_debug.h"

#include <klocale.h>

#include <tqfile.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

using Tellico::Cite::Lyxpipe;

Lyxpipe::Lyxpipe() : Action() {
}

bool Lyxpipe::cite(Data::EntryVec entries_) {
  if(entries_.isEmpty()) {
    return false;
  }

  Data::CollPtr coll = entries_.front()->collection();
  if(!coll || coll->type() != Data::Collection::Bibtex) {
    myDebug() << "Lyxpipe::cite() - collection must be a bibliography!" << endl;
    return false;
  }

  TQString lyxpipe = Config::lyxpipe();
  lyxpipe += TQString::tqfromLatin1(".in");
//  myDebug() << "Lyxpipe::cite() - " << lyxpipe << endl;

  TQString errorStr = i18n("<qt>Tellico is unable to write to the server pipe at <b>%1</b>.</qt>").tqarg(lyxpipe);

  TQFile file(lyxpipe);
  if(!file.exists()) {
    Kernel::self()->sorry(errorStr);
    return false;
  }

  int pipeFd = ::open(TQFile::encodeName(lyxpipe), O_WRONLY);
  if(!file.open(IO_WriteOnly, pipeFd)) {
    Kernel::self()->sorry(errorStr);
    ::close(pipeFd);
    return false;
  }

  TQString output;
  TQTextStream ts(&file);
  for(Data::EntryVecIt it = entries_.begin(); it != entries_.end(); ++it) {
    TQString s = BibtexHandler::bibtexKey(it.data());
    if(s.isEmpty()) {
      continue;
    }
    output += s;
    if(!it.nextEnd() && !output.isEmpty()) {
      // pybliographer uses comma-space, and pyblink expects the space there
      output += TQString::tqfromLatin1(", ");
    }
  }
  if(output.isEmpty()) {
    myDebug() << "Lyxpipe::cite() - no available bibtex keys!" << endl;
    return false;
  }

//  ts << "LYXSRV:tellico:hello\n";
  ts << "LYXCMD:tellico:citation-insert:";
  ts << output.local8Bit();
  ts << "\n";
//  ts << "LYXSRV:tellico:bye\n";
  file.flush();
  file.close();
  ::close(pipeFd);
  return true;
}