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
26 #include <sys/types.h>
28 #include "bufferlist.h"
30 #include "minibuffer.h"
31 #include "support/FileInfo.h"
32 #include "support/filetools.h"
33 #include "lyx_gui_misc.h"
34 #include "lastfiles.h"
37 #include "lyxscreen.h"
43 extern BufferView * current_view;
44 extern int RunLinuxDoc(int, string const &);
47 // Class BufferStorage
50 void BufferStorage::release(Buffer * buf)
52 for(Container::iterator it = container.begin();
53 it != container.end(); ++it) {
55 Buffer * tmpbuf = (*it);
64 Buffer * BufferStorage::newBuffer(string const & s,
68 Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
69 tmpbuf->params.useClassDefaults();
70 lyxerr.debug() << "Assigning to buffer "
71 << container.size() + 1 << endl;
72 container.push_back(tmpbuf);
81 BufferList::BufferList()
83 _state = BufferList::OK;
87 bool BufferList::empty()
89 return bstore.empty();
93 extern void MenuWrite(Buffer *);
95 bool BufferList::QwriteAll()
97 bool askMoreConfirmation = false;
99 for(BufferStorage::iterator it = bstore.begin();
100 it != bstore.end(); ++it) {
101 if (!(*it)->isLyxClean()) {
102 switch(AskConfirmation(_("Changes in document:"),
103 MakeDisplayPath((*it)->fileName(),
105 _("Save document?"))) {
110 askMoreConfirmation = true;
111 unsaved += MakeDisplayPath((*it)->fileName(),
120 if (askMoreConfirmation &&
121 lyxrc->exit_confirmation &&
122 !AskQuestion(_("Some documents were not saved:"),
123 unsaved, _("Exit anyway?"))) {
131 // Should probably be moved to somewhere else: BufferView? LyXView?
132 bool BufferList::write(Buffer * buf, bool makeBackup)
134 current_view->owner()->getMiniBuffer()->Set(_("Saving document"),
135 MakeDisplayPath(buf->fileName()), "...");
137 // We don't need autosaves in the immediate future. (Asger)
138 buf->resetAutosaveTimers();
142 string s = buf->fileName() + '~';
143 // Rename is the wrong way of making a backup,
144 // this is the correct way.
145 /* truss cp fil fil2:
146 lstat("LyXVC3.lyx", 0xEFFFF898) Err#2 ENOENT
147 stat("LyXVC.lyx", 0xEFFFF688) = 0
148 open("LyXVC.lyx", O_RDONLY) = 3
149 open("LyXVC3.lyx", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 4
150 fstat(4, 0xEFFFF508) = 0
151 fstat(3, 0xEFFFF508) = 0
152 read(3, " # T h i s f i l e w".., 8192) = 5579
153 write(4, " # T h i s f i l e w".., 5579) = 5579
154 read(3, 0xEFFFD4A0, 8192) = 0
157 chmod("LyXVC3.lyx", 0100644) = 0
158 lseek(0, 0, SEEK_CUR) = 46440
162 // Should proabaly have some more error checking here.
163 // Should be cleaned up in 0.13, at least a bit.
164 // Doing it this way, also makes the inodes stay the same.
165 // This is still not a very good solution, in particular we
166 // might loose the owner of the backup.
167 FileInfo finfo(buf->fileName());
169 mode_t fmode = finfo.getMode();
170 struct utimbuf * times = new struct utimbuf;
172 times->actime = finfo.getAccessTime();
173 times->modtime = finfo.getModificationTime();
174 long blksize = finfo.getBlockSize();
175 lyxerr.debug() << "BlockSize: " << blksize << endl;
176 ifstream ifs(buf->fileName().c_str());
177 ofstream ofs(s.c_str(), ios::out|ios::trunc);
185 chmod(s.c_str(), fmode);
187 if (utime(s.c_str(), times)) {
188 lyxerr << "utime error." << endl;
191 lyxerr << "LyX was not able to make "
192 "backupcopy. Beware." << endl;
198 if (buf->writeFile(buf->fileName(), false)) {
201 current_view->owner()->getMiniBuffer()->
202 Set(_("Document saved as"),
203 MakeDisplayPath(buf->fileName()));
205 // now delete the autosavefile
206 string a = OnlyPath(buf->fileName());
208 a += OnlyFilename(buf->fileName());
210 FileInfo fileinfo(a);
211 if (fileinfo.exist()) {
212 if (remove(a.c_str()) != 0) {
213 WriteFSAlert(_("Could not delete "
214 "auto-save file!"), a);
218 // Saving failed, so backup is not backup
220 string s = buf->fileName() + '~';
221 rename(s.c_str(), buf->fileName().c_str());
223 current_view->owner()->getMiniBuffer()->Set(_("Save failed!"));
231 void BufferList::closeAll()
233 _state = BufferList::CLOSING;
234 while (!bstore.empty()) {
235 close(bstore.front());
237 _state = BufferList::OK;
241 void BufferList::resize()
243 for(BufferStorage::iterator it = bstore.begin();
244 it != bstore.end(); ++it) {
250 bool BufferList::close(Buffer * buf)
252 if (buf->getUser()) buf->getUser()->insetUnlock();
254 if (buf->paragraph && !buf->isLyxClean() && !quitting) {
256 switch(AskConfirmation(_("Changes in document:"),
257 MakeDisplayPath(buf->fileName(), 50),
258 _("Save document?"))){
261 lastfiles->newFile(buf->fileName());
279 void BufferList::makePup(int pup)
280 /* This should be changed to return a char const *const
281 in the same way as for lastfiles.[hC]
285 for(BufferStorage::iterator it = bstore.begin();
286 it != bstore.end(); ++it) {
287 string relbuf = MakeDisplayPath((*it)->fileName(), 30);
288 fl_addtopup(pup, relbuf.c_str());
291 if (ant == 0) fl_addtopup(pup, _("No Documents Open!%t"));
295 Buffer * BufferList::first()
297 if (bstore.empty()) return 0;
298 return bstore.front();
302 Buffer * BufferList::getBuffer(int choice)
304 if (choice >= bstore.size()) return 0;
305 return bstore[choice];
309 void BufferList::updateInset(Inset * inset, bool mark_dirty)
311 for (BufferStorage::iterator it = bstore.begin();
312 it != bstore.end(); ++it) {
314 && (*it)->getUser()->text->UpdateInset(inset)) {
323 int BufferList::unlockInset(UpdatableInset * inset)
325 if (!inset) return 1;
326 for(BufferStorage::iterator it = bstore.begin();
327 it != bstore.end(); ++it) {
329 && (*it)->getUser()->the_locking_inset == inset) {
330 (*it)->getUser()->insetUnlock();
338 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
340 for(BufferStorage::iterator it = bstore.begin();
341 it != bstore.end(); ++it) {
342 if (!(*it)->isDepClean(mastertmpdir)) {
343 string writefile = mastertmpdir;
345 writefile += ChangeExtension((*it)->fileName(),
347 (*it)->makeLaTeXFile(writefile, mastertmpdir,
349 (*it)->markDepClean(mastertmpdir);
355 void BufferList::emergencyWriteAll()
357 for (BufferStorage::iterator it = bstore.begin();
358 it != bstore.end(); ++it) {
359 if (!(*it)->isLyxClean()) {
362 lyxerr <<_("lyx: Attempting to save"
365 << _(" as...") << endl;
367 for (int i = 0; i < 3 && !madeit; ++i) {
370 // We try to save three places:
371 // 1) Same place as document.
372 // 2) In HOME directory.
373 // 3) In "/tmp" directory.
375 s = (*it)->fileName();
377 s = AddName(GetEnvPath("HOME"),
380 // MakeAbsPath to prepend the current
381 // drive letter on OS/2
382 s = AddName(MakeAbsPath("/tmp/"),
387 lyxerr << " " << i + 1 << ") " << s << endl;
389 if ((*it)->writeFile(s, true)) {
390 (*it)->markLyxClean();
391 lyxerr << _(" Save seems successful. "
395 lyxerr << _(" Save failed! Trying...")
398 lyxerr << _(" Save failed! Bummer. "
408 Buffer * BufferList::readFile(string const & s, bool ronly)
410 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
413 string e = OnlyPath(s);
415 // File information about normal file
416 FileInfo fileInfo2(s);
418 // Check if emergency save file exists and is newer.
419 e += OnlyFilename(s) + ".emergency";
420 FileInfo fileInfoE(e);
422 bool use_emergency = false;
424 if (fileInfoE.exist() && fileInfo2.exist()) {
425 if (fileInfoE.getModificationTime()
426 > fileInfo2.getModificationTime()) {
427 if (AskQuestion(_("An emergency save of this document exists!"),
428 MakeDisplayPath(s, 50),
429 _("Try to load that instead?"))) {
431 // the file is not saved if we load the
434 use_emergency = true;
436 // Here, we should delete the emergency save
442 if (!use_emergency) {
443 // Now check if autosave file is newer.
445 a += OnlyFilename(s);
447 FileInfo fileInfoA(a);
448 if (fileInfoA.exist() && fileInfo2.exist()) {
449 if (fileInfoA.getModificationTime()
450 > fileInfo2.getModificationTime()) {
451 if (AskQuestion(_("Autosave file is newer."),
452 MakeDisplayPath(s, 50),
453 _("Load that one instead?"))) {
455 // the file is not saved if we load the
459 // Here, we should delete the autosave
465 // not sure if this is the correct place to begin LyXLex
468 if (b->readFile(lex))
477 bool BufferList::exists(string const & s)
479 for (BufferStorage::iterator it = bstore.begin();
480 it != bstore.end(); ++it) {
481 if ((*it)->fileName() == s)
488 Buffer * BufferList::getBuffer(string const & s)
490 for(BufferStorage::iterator it = bstore.begin();
491 it != bstore.end(); ++it) {
492 if ((*it)->fileName() == s)
499 Buffer * BufferList::newFile(string const & name, string tname)
501 /* get a free buffer */
502 Buffer * b = bstore.newBuffer(name, lyxrc);
504 // use defaults.lyx as a default template if it exists.
506 tname = LibFileSearch("templates", "defaults.lyx");
508 if (!tname.empty() && IsLyXFilename(tname)){
509 bool templateok = false;
513 if (b->readFile(lex)) {
518 WriteAlert(_("Error!"), _("Unable to open template"),
519 MakeDisplayPath(tname));
520 // no template, start with empty buffer
521 b->paragraph = new LyXParagraph;
524 else { // start with empty buffer
525 b->paragraph = new LyXParagraph;
529 b->setReadonly(false);
535 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
537 // make sure our path is absolute
538 string s = MakeAbsPath(filename);
540 // Is this done too early?
542 if (IsSGMLFilename(s)) {
544 if (fi.exist() && fi.readable()) {
545 if (!RunLinuxDoc(-1, s)) {
546 s = ChangeExtension (s, ".lyx", false);
547 } else { // sgml2lyx failed
548 WriteAlert(_("Error!"),
549 _("Could not convert file"), s);
553 // just change the extension and it will be
554 // handled like a regular lyx file that does
556 s = ChangeExtension(s, ".lyx", false);
560 // file already open?
562 if (AskQuestion(_("Document is already open:"),
563 MakeDisplayPath(s, 50),
564 _("Do you want to reload that document?"))) {
565 // Reload is accomplished by closing and then loading
566 if (!close(getBuffer(s))) {
569 // Fall through to new load. (Asger)
571 // Here, we pretend that we just loaded the
578 switch (IsFileWriteable(s)) {
580 current_view->owner()->getMiniBuffer()->
581 Set(_("File `")+MakeDisplayPath(s, 50)+
582 _("' is read-only."));
588 b->lyxvc.file_found_hook(s);
590 break; //fine- it's r/w
592 // Here we probably should run
593 if (LyXVC::file_not_found_hook(s)) {
594 // Ask if the file should be checked out for
595 // viewing/editing, if so: load it.
596 lyxerr << "Do you want to checkout?" << endl;
598 if (AskQuestion(_("Cannot open specified file:"),
599 MakeDisplayPath(s, 50),
600 _("Create new document with this name?")))
602 // Find a free buffer
603 b = newFile(s, string());
608 if (b && tolastfiles)
609 lastfiles->newFile(b->fileName());