1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Word Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-1999 The LyX Team.
9 * This file is Copyright 1996-1999
12 * ======================================================
16 #pragma implementation
20 #include <sys/types.h>
22 #include "bufferlist.h"
24 #include "minibuffer.h"
25 #include "support/FileInfo.h"
26 #include "support/filetools.h"
27 #include "lyx_gui_misc.h"
28 #include "lastfiles.h"
31 #include "lyxscreen.h"
37 extern BufferView * current_view;
38 extern void BeforeChange();
39 extern int RunLinuxDoc(int, string const &);
42 // Class BufferStorage
45 void BufferStorage::release(Buffer * buf)
47 for(Container::iterator it = container.begin();
48 it != container.end(); ++it) {
50 Buffer * tmpbuf = (*it);
59 Buffer * BufferStorage::newBuffer(string const & s,
63 Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
64 tmpbuf->params.useClassDefaults();
65 lyxerr.debug() << "Assigning to buffer "
66 << container.size() + 1 << endl;
67 container.push_back(tmpbuf);
76 BufferList::BufferList()
78 _state = BufferList::OK;
82 bool BufferList::empty()
84 return bstore.empty();
88 extern void MenuWrite(Buffer *);
90 bool BufferList::QwriteAll()
92 bool askMoreConfirmation = false;
94 for(BufferStorage::iterator it = bstore.begin();
95 it != bstore.end(); ++it) {
96 if (!(*it)->isLyxClean()) {
97 switch(AskConfirmation(_("Changes in document:"),
98 MakeDisplayPath((*it)->fileName(),
100 _("Save document?"))) {
105 askMoreConfirmation = true;
106 unsaved += MakeDisplayPath((*it)->fileName(),
115 if (askMoreConfirmation &&
116 lyxrc->exit_confirmation &&
117 !AskQuestion(_("Some documents were not saved:"),
118 unsaved, _("Exit anyway?"))) {
126 // Should probably be moved to somewhere else: BufferView? LyXView?
127 bool BufferList::write(Buffer * buf, bool makeBackup)
129 current_view->owner()->getMiniBuffer()->Set(_("Saving document"),
130 MakeDisplayPath(buf->fileName()), "...");
132 // We don't need autosaves in the immediate future. (Asger)
133 buf->resetAutosaveTimers();
137 string s = buf->fileName() + '~';
138 // Rename is the wrong way of making a backup,
139 // this is the correct way.
140 /* truss cp fil fil2:
141 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
142 stat("LyXVC.lyx", 0xEFFFF688) = 0
143 open("LyXVC.lyx", O_RDONLY) = 3
144 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
145 fstat(4, 0xEFFFF508) = 0
146 fstat(3, 0xEFFFF508) = 0
147 read(3, " # T h i s f i l e w".., 8192) = 5579
148 write(4, " # T h i s f i l e w".., 5579) = 5579
149 read(3, 0xEFFFD4A0, 8192) = 0
152 chmod("LyXVC3.lyx", 0100644) = 0
153 lseek(0, 0, SEEK_CUR) = 46440
157 // Should proabaly have some more error checking here.
158 // Should be cleaned up in 0.13, at least a bit.
159 // Doing it this way, also makes the inodes stay the same.
160 // This is still not a very good solution, in particular we
161 // might loose the owner of the backup.
162 FileInfo finfo(buf->fileName());
164 mode_t fmode = finfo.getMode();
165 struct utimbuf * times = new struct utimbuf;
167 times->actime = finfo.getAccessTime();
168 times->modtime = finfo.getModificationTime();
169 long blksize = finfo.getBlockSize();
170 lyxerr.debug() << "BlockSize: " << blksize << endl;
171 ifstream ifs(buf->fileName().c_str());
172 ofstream ofs(s.c_str(), ios::out|ios::trunc);
180 chmod(s.c_str(), fmode);
182 if (utime(s.c_str(), times)) {
183 lyxerr << "utime error." << endl;
186 lyxerr << "LyX was not able to make "
187 "backupcopy. Beware." << endl;
193 if (buf->writeFile(buf->fileName(), false)) {
196 current_view->owner()->getMiniBuffer()->
197 Set(_("Document saved as"),
198 MakeDisplayPath(buf->fileName()));
200 // now delete the autosavefile
201 string a = OnlyPath(buf->fileName());
203 a += OnlyFilename(buf->fileName());
205 FileInfo fileinfo(a);
206 if (fileinfo.exist()) {
207 if (remove(a.c_str()) != 0) {
208 WriteFSAlert(_("Could not delete "
209 "auto-save file!"), a);
213 // Saving failed, so backup is not backup
215 string s = buf->fileName() + '~';
216 rename(s.c_str(), buf->fileName().c_str());
218 current_view->owner()->getMiniBuffer()->Set(_("Save failed!"));
226 void BufferList::closeAll()
228 _state = BufferList::CLOSING;
229 while (!bstore.empty()) {
230 close(bstore.front());
232 _state = BufferList::OK;
236 void BufferList::resize()
238 for(BufferStorage::iterator it = bstore.begin();
239 it != bstore.end(); ++it) {
245 bool BufferList::close(Buffer * buf)
249 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
251 switch(AskConfirmation(_("Changes in document:"),
252 MakeDisplayPath(buf->fileName(), 50),
253 _("Save document?"))){
256 lastfiles->newFile(buf->fileName());
274 void BufferList::makePup(int pup)
275 /* This should be changed to return a char const *const
276 in the same way as for lastfiles.[hC]
280 for(BufferStorage::iterator it = bstore.begin();
281 it != bstore.end(); ++it) {
282 string relbuf = MakeDisplayPath((*it)->fileName(), 30);
283 fl_addtopup(pup, relbuf.c_str());
286 if (ant == 0) fl_addtopup(pup, _("No Documents Open!%t"));
290 Buffer * BufferList::first()
292 if (bstore.empty()) return 0;
293 return bstore.front();
297 Buffer * BufferList::getBuffer(int choice)
299 if (choice >= bstore.size()) return 0;
300 return bstore[choice];
304 void BufferList::updateInset(Inset * inset, bool mark_dirty)
306 for (BufferStorage::iterator it = bstore.begin();
307 it != bstore.end(); ++it) {
309 && (*it)->getUser()->text->UpdateInset(inset)) {
318 int BufferList::unlockInset(UpdatableInset * inset)
320 if (!inset) return 1;
321 for(BufferStorage::iterator it = bstore.begin();
322 it != bstore.end(); ++it) {
323 if ((*it)->the_locking_inset == inset) {
324 (*it)->InsetUnlock();
332 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
334 for(BufferStorage::iterator it = bstore.begin();
335 it != bstore.end(); ++it) {
336 if (!(*it)->isDepClean(mastertmpdir)) {
337 string writefile = mastertmpdir;
339 writefile += ChangeExtension((*it)->fileName(),
341 (*it)->makeLaTeXFile(writefile, mastertmpdir,
343 (*it)->markDepClean(mastertmpdir);
349 void BufferList::emergencyWriteAll()
351 for (BufferStorage::iterator it = bstore.begin();
352 it != bstore.end(); ++it) {
353 if (!(*it)->isLyxClean()) {
356 lyxerr <<_("lyx: Attempting to save"
359 << _(" as...") << endl;
361 for (int i = 0; i < 3 && !madeit; ++i) {
364 // We try to save three places:
365 // 1) Same place as document.
366 // 2) In HOME directory.
367 // 3) In "/tmp" directory.
369 s = (*it)->fileName();
371 s = AddName(GetEnvPath("HOME"),
374 // MakeAbsPath to prepend the current
375 // drive letter on OS/2
376 s = AddName(MakeAbsPath("/tmp/"),
381 lyxerr << " " << i + 1 << ") " << s << endl;
383 if ((*it)->writeFile(s, true)) {
384 (*it)->markLyxClean();
385 lyxerr << _(" Save seems successful. "
389 lyxerr << _(" Save failed! Trying...")
392 lyxerr << _(" Save failed! Bummer. "
402 Buffer * BufferList::readFile(string const & s, bool ronly)
404 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
407 string e = OnlyPath(s);
409 // File information about normal file
410 FileInfo fileInfo2(s);
412 // Check if emergency save file exists and is newer.
413 e += OnlyFilename(s) + ".emergency";
414 FileInfo fileInfoE(e);
416 bool use_emergency = false;
418 if (fileInfoE.exist() && fileInfo2.exist()) {
419 if (fileInfoE.getModificationTime()
420 > fileInfo2.getModificationTime()) {
421 if (AskQuestion(_("An emergency save of this document exists!"),
422 MakeDisplayPath(s, 50),
423 _("Try to load that instead?"))) {
425 // the file is not saved if we load the
428 use_emergency = true;
430 // Here, we should delete the emergency save
436 if (!use_emergency) {
437 // Now check if autosave file is newer.
439 a += OnlyFilename(s);
441 FileInfo fileInfoA(a);
442 if (fileInfoA.exist() && fileInfo2.exist()) {
443 if (fileInfoA.getModificationTime()
444 > fileInfo2.getModificationTime()) {
445 if (AskQuestion(_("Autosave file is newer."),
446 MakeDisplayPath(s, 50),
447 _("Load that one instead?"))) {
449 // the file is not saved if we load the
453 // Here, we should delete the autosave
459 // not sure if this is the correct place to begin LyXLex
462 if (b->readFile(lex))
471 bool BufferList::exists(string const & s)
473 for (BufferStorage::iterator it = bstore.begin();
474 it != bstore.end(); ++it) {
475 if ((*it)->fileName() == s)
482 Buffer * BufferList::getBuffer(string const & s)
484 for(BufferStorage::iterator it = bstore.begin();
485 it != bstore.end(); ++it) {
486 if ((*it)->fileName() == s)
493 Buffer * BufferList::newFile(string const & name, string tname)
495 /* get a free buffer */
496 Buffer * b = bstore.newBuffer(name, lyxrc);
498 // use defaults.lyx as a default template if it exists.
500 tname = LibFileSearch("templates", "defaults.lyx");
502 if (!tname.empty() && IsLyXFilename(tname)){
503 bool templateok = false;
507 if (b->readFile(lex)) {
512 WriteAlert(_("Error!"), _("Unable to open template"),
513 MakeDisplayPath(tname));
514 // no template, start with empty buffer
515 b->paragraph = new LyXParagraph;
518 else { // start with empty buffer
519 b->paragraph = new LyXParagraph;
523 b->setReadonly(false);
529 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
531 // make sure our path is absolute
532 string s = MakeAbsPath(filename);
534 // Is this done too early?
536 if (IsSGMLFilename(s)) {
538 if (fi.exist() && fi.readable()) {
539 if (!RunLinuxDoc(-1, s)) {
540 s = ChangeExtension (s, ".lyx", false);
541 } else { // sgml2lyx failed
542 WriteAlert(_("Error!"),
543 _("Could not convert file"), s);
547 // just change the extension and it will be
548 // handled like a regular lyx file that does
550 s = ChangeExtension(s, ".lyx", false);
554 // file already open?
556 if (AskQuestion(_("Document is already open:"),
557 MakeDisplayPath(s, 50),
558 _("Do you want to reload that document?"))) {
559 // Reload is accomplished by closing and then loading
560 if (!close(getBuffer(s))) {
563 // Fall through to new load. (Asger)
565 // Here, we pretend that we just loaded the
572 switch (IsFileWriteable(s)) {
574 current_view->owner()->getMiniBuffer()->
575 Set(_("File `")+MakeDisplayPath(s, 50)+
576 _("' is read-only."));
582 b->lyxvc.file_found_hook(s);
584 break; //fine- it's r/w
586 // Here we probably should run
587 if (LyXVC::file_not_found_hook(s)) {
588 // Ask if the file should be checked out for
589 // viewing/editing, if so: load it.
590 lyxerr << "Do you want to checkout?" << endl;
592 if (AskQuestion(_("Cannot open specified file:"),
593 MakeDisplayPath(s, 50),
594 _("Create new document with this name?")))
596 // Find a free buffer
597 b = newFile(s, string());
602 if (b && tolastfiles)
603 lastfiles->newFile(b->fileName());