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"
36 extern BufferView * current_view;
37 extern MiniBuffer * minibuffer;
38 extern void SmallUpdate(signed char);
39 extern void BeforeChange();
40 extern int RunLinuxDoc(int, string const &);
43 // Class BufferStorage
46 void BufferStorage::release(Buffer * buf)
48 for(Container::iterator it = container.begin();
49 it != container.end(); ++it) {
51 Buffer * tmpbuf = (*it);
60 Buffer * BufferStorage::newBuffer(string const & s,
64 Buffer * tmpbuf = new Buffer(s, lyxrc, ronly);
65 tmpbuf->params.useClassDefaults();
66 lyxerr.debug() << "Assigning to buffer "
67 << container.size() + 1 << endl;
68 container.push_back(tmpbuf);
77 BufferList::BufferList()
79 _state = BufferList::OK;
83 bool BufferList::empty()
85 return bstore.empty();
89 extern void MenuWrite(Buffer *);
91 bool BufferList::QwriteAll()
93 bool askMoreConfirmation = false;
95 for(BufferStorage::iterator it = bstore.begin();
96 it != bstore.end(); ++it) {
97 if (!(*it)->isLyxClean()) {
98 switch(AskConfirmation(_("Changes in document:"),
99 MakeDisplayPath((*it)->filename,
101 _("Save document?"))) {
106 askMoreConfirmation = true;
107 unsaved += MakeDisplayPath((*it)->filename, 50);
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 minibuffer->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();
166 struct utimbuf *times =
167 (struct utimbuf*)new char[sizeof(struct utimbuf)];
168 times->actime = finfo.getAccessTime();
169 times->modtime = finfo.getModificationTime();
170 long blksize = finfo.getBlockSize();
171 lyxerr.debug() << "BlockSize: " << blksize << endl;
172 FilePtr fin(buf->filename, FilePtr::read);
173 FilePtr fout(s, FilePtr::truncate);
174 if (fin() && fout()) {
175 char * cbuf = new char[blksize+1];
179 c_read = fread(cbuf, 1, blksize, fin);
182 fwrite(cbuf, 1, c_read, fout);
186 chmod(s.c_str(), fmode);
188 if (utime(s.c_str(), times)) {
189 lyxerr << "utime error." << endl;
193 lyxerr << "LyX was not able to make backupcopy. Beware." << endl;
199 if (buf->writeFile(buf->filename, false)) {
202 minibuffer->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 minibuffer->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)
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) {
313 if ((*it)->text && (*it)->text->UpdateInset(inset)) {
322 int BufferList::unlockInset(UpdatableInset * inset)
324 if (!inset) return 1;
325 for(BufferStorage::iterator it = bstore.begin();
326 it != bstore.end(); ++it) {
327 if ((*it)->the_locking_inset == inset) {
328 (*it)->InsetUnlock();
336 void BufferList::updateIncludedTeXfiles(string const & mastertmpdir)
338 for(BufferStorage::iterator it = bstore.begin();
339 it != bstore.end(); ++it) {
340 if (!(*it)->isDepClean(mastertmpdir)) {
341 string writefile = mastertmpdir;
343 writefile += ChangeExtension((*it)->getFileName(),
345 (*it)->makeLaTeXFile(writefile, mastertmpdir,
347 (*it)->markDepClean(mastertmpdir);
354 void BufferList::emergencyWriteAll()
356 for (BufferStorage::iterator it = bstore.begin();
357 it != bstore.end(); ++it) {
358 if (!(*it)->isLyxClean()) {
361 lyxerr <<_("lyx: Attempting to save"
364 << _(" as...") << endl;
366 for (int i = 0; i < 3 && !madeit; ++i) {
369 // We try to save three places:
370 // 1) Same place as document.
371 // 2) In HOME directory.
372 // 3) In "/tmp" directory.
376 s = AddName(GetEnvPath("HOME"),
378 } else { // MakeAbsPath to prepend the current drive letter on OS/2
379 s = AddName(MakeAbsPath("/tmp/"),
384 lyxerr << " " << i + 1 << ") " << s << endl;
386 if ((*it)->writeFile(s, true)) {
387 (*it)->markLyxClean();
388 lyxerr << _(" Save seems successful. "
392 lyxerr << _(" Save failed! Trying...")
395 lyxerr << _(" Save failed! Bummer. Document is lost.") << endl;
403 Buffer * BufferList::readFile(string const & s, bool ronly)
405 Buffer * b = bstore.newBuffer(s, lyxrc, ronly);
408 string e = OnlyPath(s);
410 // File information about normal file
411 FileInfo fileInfo2(s);
413 // Check if emergency save file exists and is newer.
414 e += OnlyFilename(s) + ".emergency";
415 FileInfo fileInfoE(e);
417 bool use_emergency = false;
419 if (fileInfoE.exist() && fileInfo2.exist()) {
420 if (fileInfoE.getModificationTime()
421 > fileInfo2.getModificationTime()) {
422 if (AskQuestion(_("An emergency save of this document exists!"),
423 MakeDisplayPath(s, 50),
424 _("Try to load that instead?"))) {
426 // the file is not saved if we load the
429 use_emergency = true;
431 // Here, we should delete the emergency save
437 if (!use_emergency) {
438 // Now check if autosave file is newer.
440 a += OnlyFilename(s);
442 FileInfo fileInfoA(a);
443 if (fileInfoA.exist() && fileInfo2.exist()) {
444 if (fileInfoA.getModificationTime()
445 > fileInfo2.getModificationTime()) {
446 if (AskQuestion(_("Autosave file is newer."),
447 MakeDisplayPath(s, 50),
448 _("Load that one instead?"))) {
450 // the file is not saved if we load the
454 // Here, we should delete the autosave
460 // not sure if this is the correct place to begin LyXLex
463 if (b->readFile(lex))
472 bool BufferList::exists(string const & s)
474 for (BufferStorage::iterator it = bstore.begin();
475 it != bstore.end(); ++it) {
476 if ((*it)->filename == s)
483 Buffer * BufferList::getBuffer(string const & s)
485 for(BufferStorage::iterator it = bstore.begin();
486 it != bstore.end(); ++it) {
487 if ((*it)->filename == s)
494 Buffer * BufferList::newFile(string const & name, string tname)
496 /* get a free buffer */
497 Buffer * b = bstore.newBuffer(name, lyxrc);
499 // use defaults.lyx as a default template if it exists.
501 tname = LibFileSearch("templates", "defaults.lyx");
503 if (!tname.empty() && IsLyXFilename(tname)){
504 bool templateok = false;
508 if (b->readFile(lex)) {
513 WriteAlert(_("Error!"), _("Unable to open template"),
514 MakeDisplayPath(tname));
515 // no template, start with empty buffer
516 b->paragraph = new LyXParagraph;
519 else { // start with empty buffer
520 b->paragraph = new LyXParagraph;
524 b->setReadonly(false);
530 Buffer * BufferList::loadLyXFile(string const & filename, bool tolastfiles)
532 // make sure our path is absolute
533 string s = MakeAbsPath(filename);
535 // Is this done too early?
537 if (IsSGMLFilename(s)) {
539 if (fi.exist() && fi.readable()) {
540 if (!RunLinuxDoc(-1, s)) {
541 s = ChangeExtension (s, ".lyx", false);
542 } else { // sgml2lyx failed
543 WriteAlert(_("Error!"),
544 _("Could not convert file"), s);
548 // just change the extension and it will be
549 // handled like a regular lyx file that does
551 s = ChangeExtension(s, ".lyx", false);
555 // file already open?
557 if (AskQuestion(_("Document is already open:"),
558 MakeDisplayPath(s, 50),
559 _("Do you want to reload that document?"))) {
560 // Reload is accomplished by closing and then loading
561 if (!close(getBuffer(s))) {
564 // Fall through to new load. (Asger)
566 // Here, we pretend that we just loaded the
573 switch (IsFileWriteable(s)) {
575 minibuffer->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->getFileName());